Skip to content

Commit

Permalink
fix: don't return 500 on conflict for POST /admin/identities (#3437)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr authored Aug 16, 2023
1 parent 44f1e70 commit 1429949
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
17 changes: 17 additions & 0 deletions identity/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0

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, errors.WithStack(herodot.ErrConflict.WithReason("This identity conflicts with another identity that already exists.")))
} 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 1429949

Please sign in to comment.