graph/education: Fix issues reported by sonarcloud

This commit is contained in:
Ralf Haferkamp
2023-09-27 14:04:46 +02:00
committed by Ralf Haferkamp
parent a34d467285
commit 4465c9385d
3 changed files with 12 additions and 5 deletions

View File

@@ -171,7 +171,7 @@ func (i *LDAP) CreateEducationSchool(ctx context.Context, school libregraph.Educ
}
// UpdateEducationSchoolOperation contains the logic for which update operation to apply to a school
func (i *LDAP) UpdateEducationSchoolOperation(
func (i *LDAP) updateEducationSchoolOperation(
schoolUpdate libregraph.EducationSchool,
currentSchool libregraph.EducationSchool,
) schoolUpdateOperation {
@@ -289,7 +289,7 @@ func (i *LDAP) UpdateEducationSchool(ctx context.Context, numberOrID string, sch
}
currentSchool := i.createSchoolModelFromLDAP(e)
switch i.UpdateEducationSchoolOperation(school, *currentSchool) {
switch i.updateEducationSchoolOperation(school, *currentSchool) {
case tooManyValues:
return nil, fmt.Errorf("school name and school number cannot be updated in the same request")
case schoolUnchanged:

View File

@@ -310,7 +310,7 @@ func TestUpdateEducationSchoolOperation(t *testing.T) {
SchoolNumber: &tt.schoolNumber,
}
operation := b.UpdateEducationSchoolOperation(schoolUpdate, currentSchool)
operation := b.updateEducationSchoolOperation(schoolUpdate, currentSchool)
assert.Equal(t, tt.expectedOperation, operation)
}
}

View File

@@ -1,3 +1,4 @@
// Package errorcode allows to deal with graph error codes
package errorcode
import (
@@ -13,6 +14,7 @@ import (
// ErrorCode defines code as used in MS Graph - see https://docs.microsoft.com/en-us/graph/errors?context=graph%2Fapi%2F1.0&view=graph-rest-1.0
type ErrorCode int
// Error defines a custom error struct, containing and MS Graph error code an a textual error message
type Error struct {
errorCode ErrorCode
msg string
@@ -79,6 +81,7 @@ var errorCodes = [...]string{
"preconditionFailed",
}
// New constructs a new errorcode.Error
func New(e ErrorCode, msg string) Error {
return Error{
errorCode: e,
@@ -86,7 +89,7 @@ func New(e ErrorCode, msg string) Error {
}
}
// Render writes an Graph ErrorObject to the response writer
// Render writes an Graph ErrorCode object to the response writer
func (e ErrorCode) Render(w http.ResponseWriter, r *http.Request, status int, msg string) {
innererror := map[string]interface{}{
"date": time.Now().UTC().Format(time.RFC3339),
@@ -104,12 +107,14 @@ func (e ErrorCode) Render(w http.ResponseWriter, r *http.Request, status int, ms
render.JSON(w, r, resp)
}
// Render writes an Graph Error object to the response writer
func (e Error) Render(w http.ResponseWriter, r *http.Request) {
var status int
switch e.errorCode {
case AccessDenied:
status = http.StatusForbidden
case InvalidRange:
case
InvalidRange:
status = http.StatusRequestedRangeNotSatisfiable
case InvalidRequest:
status = http.StatusBadRequest
@@ -125,10 +130,12 @@ func (e Error) Render(w http.ResponseWriter, r *http.Request) {
e.errorCode.Render(w, r, status, e.msg)
}
// String returns the string corresponding to the ErrorCode
func (e ErrorCode) String() string {
return errorCodes[e]
}
// Error return the concatenation of the error string and optinal message
func (e Error) Error() string {
errString := errorCodes[e.errorCode]
if e.msg != "" {