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 6d49450
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions services/graph/pkg/identity/ldap_education_school.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
11 changes: 9 additions & 2 deletions services/graph/pkg/service/v0/errorcode/errorcode.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package errorcode allows to deal with graph error codes
package errorcode

import (
Expand All @@ -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
Expand Down Expand Up @@ -79,14 +81,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 +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
Expand All @@ -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 != "" {
Expand Down

0 comments on commit 6d49450

Please sign in to comment.