Merge pull request #5682 from owncloud/graph-education-errors

make graph/education API errors more consistent
This commit is contained in:
David Christofas
2023-02-28 20:49:29 +01:00
committed by GitHub
5 changed files with 30 additions and 2 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}