graph/education: Fix issues reported by sonarcloud

This commit is contained in:
Ralf Haferkamp
2023-09-27 15:39:00 +02:00
committed by Ralf Haferkamp
parent a34d467285
commit 4465c9385d
3 changed files with 12 additions and 5 deletions
@@ -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 != "" {