Skip to content

Commit

Permalink
Merge pull request #5682 from owncloud/graph-education-errors
Browse files Browse the repository at this point in the history
make graph/education API errors more consistent
  • Loading branch information
David Christofas authored Feb 28, 2023
2 parents e04f88f + 0fadc79 commit 0a2fc8d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/graph-education-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Make graph/education API errors more consistent

Aligned the error messages when creating schools and classes fail and changed the response code from 500 to 409.

https://github.com/owncloud/ocis/pull/5682
https://github.com/owncloud/ocis/issues/5660
7 changes: 7 additions & 0 deletions services/graph/pkg/identity/ldap_education_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ func (i *LDAP) CreateEducationClass(ctx context.Context, class libregraph.Educat
}

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

Expand Down
5 changes: 5 additions & 0 deletions services/graph/pkg/service/v0/educationclasses.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ func (g Graph) PostEducationClass(w http.ResponseWriter, r *http.Request) {

if class, err = g.identityEducationBackend.CreateEducationClass(r.Context(), *class); err != nil {
logger.Debug().Interface("class", class).Msg("could not create class: backend error")
var eerr errorcode.Error
if errors.As(err, &eerr) {
eerr.Render(w, r)
return
}
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}
Expand Down
5 changes: 5 additions & 0 deletions services/graph/pkg/service/v0/educationschools.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func (g Graph) PostEducationSchool(w http.ResponseWriter, r *http.Request) {

if school, err = g.identityEducationBackend.CreateEducationSchool(r.Context(), *school); err != nil {
logger.Debug().Err(err).Interface("school", school).Msg("could not create school: backend error")
var eerr errorcode.Error
if errors.As(err, &eerr) {
eerr.Render(w, r)
return
}
errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
return
}
Expand Down
9 changes: 7 additions & 2 deletions services/graph/pkg/service/v0/errorcode/errorcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,14 @@ func (e ErrorCode) Render(w http.ResponseWriter, r *http.Request, status int, ms
}

func (e Error) Render(w http.ResponseWriter, r *http.Request) {
status := http.StatusInternalServerError
if e.errorCode == ItemNotFound {
var status int
switch e.errorCode {
case ItemNotFound:
status = http.StatusNotFound
case NameAlreadyExists:
status = http.StatusConflict
default:
status = http.StatusInternalServerError
}
e.errorCode.Render(w, r, status, e.msg)
}
Expand Down

0 comments on commit 0a2fc8d

Please sign in to comment.