Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make graph/education API errors more consistent #5682

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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