Skip to content

Commit

Permalink
graph/education: Fix issues reported by sonarcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
rhafer committed Sep 27, 2023
1 parent ea73444 commit f9ef193
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions services/graph/pkg/identity/ldap_education_school.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion services/graph/pkg/identity/ldap_education_school_test.go
Expand Up @@ -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)
}
}
Expand Down
10 changes: 8 additions & 2 deletions services/graph/pkg/service/v0/errorcode/errorcode.go
Expand Up @@ -13,6 +13,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
Expand Down Expand Up @@ -79,14 +80,15 @@ var errorCodes = [...]string{
"preconditionFailed",
}

// New constructs a new errorcode.Error
func New(e ErrorCode, msg string) Error {
return Error{
errorCode: e,
msg: msg,
}
}

// 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),
Expand All @@ -104,12 +106,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
Expand All @@ -125,10 +129,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 != "" {
Expand Down

0 comments on commit f9ef193

Please sign in to comment.