Skip to content

Commit

Permalink
fix: don't return 500 on conflict for POST /admin/identities
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Aug 16, 2023
1 parent 44f1e70 commit edf0e3c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
14 changes: 14 additions & 0 deletions identity/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package identity

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestErrDuplicateCredentials(t *testing.T) {
inner := errors.New("inner error")
err := &ErrDuplicateCredentials{inner, nil, nil, ""}
assert.ErrorIs(t, err, inner)
}
7 changes: 6 additions & 1 deletion identity/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/ory/x/pagination/migrationpagination"
"github.com/ory/x/sqlcon"

"github.com/ory/kratos/hash"
"github.com/ory/kratos/x"
Expand Down Expand Up @@ -419,7 +420,11 @@ func (h *Handler) create(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
}

if err := h.r.IdentityManager().Create(r.Context(), i); err != nil {
h.r.Writer().WriteError(w, r, err)
if errors.Is(err, sqlcon.ErrUniqueViolation) {
h.r.Writer().WriteError(w, r, herodot.ErrConflict)
} else {
h.r.Writer().WriteError(w, r, err)
}
return
}

Expand Down
12 changes: 8 additions & 4 deletions identity/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,22 +254,26 @@ type ErrDuplicateCredentials struct {

var _ schema.DuplicateCredentialsHinter = (*ErrDuplicateCredentials)(nil)

func (e ErrDuplicateCredentials) AvailableCredentials() []string {
func (e *ErrDuplicateCredentials) Unwrap() error {
return e.error
}

func (e *ErrDuplicateCredentials) AvailableCredentials() []string {
res := make([]string, len(e.availableCredentials))
for k, v := range e.availableCredentials {
res[k] = string(v)
}
return res
}

func (e ErrDuplicateCredentials) AvailableOIDCProviders() []string {
func (e *ErrDuplicateCredentials) AvailableOIDCProviders() []string {
return e.availableOIDCProviders
}

func (e ErrDuplicateCredentials) IdentifierHint() string {
func (e *ErrDuplicateCredentials) IdentifierHint() string {
return e.identifierHint
}
func (e ErrDuplicateCredentials) HasHints() bool {
func (e *ErrDuplicateCredentials) HasHints() bool {
return len(e.availableCredentials) > 0 || len(e.availableOIDCProviders) > 0 || len(e.identifierHint) > 0
}

Expand Down

0 comments on commit edf0e3c

Please sign in to comment.