Skip to content

Commit

Permalink
Return "nameAlreadyExists" error properly
Browse files Browse the repository at this point in the history
When trying to create a user that already exist return a proper
error, that clients can check for.
  • Loading branch information
rhafer committed May 24, 2022
1 parent a6f05e7 commit d322e50
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 7 additions & 0 deletions extensions/graph/pkg/identity/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ func (i *LDAP) CreateUser(ctx context.Context, user libregraph.User) (*libregrap
ar.Attribute("sn", []string{sn})

if err := i.conn.Add(&ar); err != nil {
var lerr *ldap.Error
i.logger.Debug().Err(err).Msg("error adding user")
if errors.As(err, &lerr) {
if lerr.ResultCode == ldap.LDAPResultEntryAlreadyExists {
err = errorcode.New(errorcode.NameAlreadyExists, lerr.Error())
}
}
return nil, err
}

Expand Down
7 changes: 6 additions & 1 deletion extensions/graph/pkg/service/v0/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ func (g Graph) PostUser(w http.ResponseWriter, r *http.Request) {
}

if u, err = g.identityBackend.CreateUser(r.Context(), *u); err != nil {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
var ecErr errorcode.Error
if errors.As(err, &ecErr) {
ecErr.Render(w, r)
} else {
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
}
return
}

Expand Down

0 comments on commit d322e50

Please sign in to comment.