Files
ocis/services/graph/pkg/errorcode/errorcode.go
T

173 lines
5.4 KiB
Go
Raw Normal View History

// Package errorcode allows to deal with graph error codes
2019-12-09 15:54:14 +01:00
package errorcode
import (
"context"
"errors"
2019-12-09 15:54:14 +01:00
"net/http"
"time"
2019-12-09 15:54:14 +01:00
2021-08-24 17:48:37 +02:00
"github.com/go-chi/chi/v5/middleware"
2019-12-09 15:54:14 +01:00
"github.com/go-chi/render"
2021-12-03 09:14:22 +01:00
libregraph "github.com/owncloud/libre-graph-api-go"
2019-12-09 15:54:14 +01:00
)
// 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 and a textual error message
type Error struct {
errorCode ErrorCode
msg string
}
2023-09-08 15:40:06 +02:00
// List taken from https://github.com/microsoft/microsoft-graph-docs-1/blob/main/concepts/errors.md#code-property
2019-12-09 15:54:14 +01:00
const (
// AccessDenied defines the error if the caller doesn't have permission to perform the action.
AccessDenied ErrorCode = iota
// ActivityLimitReached defines the error if the app or user has been throttled.
ActivityLimitReached
// GeneralException defines the error if an unspecified error has occurred.
GeneralException
2021-07-09 15:15:20 +00:00
// InvalidAuthenticationToken defines the error if the access token is missing
InvalidAuthenticationToken
2019-12-09 15:54:14 +01:00
// InvalidRange defines the error if the specified byte range is invalid or unavailable.
InvalidRange
// InvalidRequest defines the error if the request is malformed or incorrect.
InvalidRequest
// ItemNotFound defines the error if the resource could not be found.
ItemNotFound
// MalwareDetected defines the error if malware was detected in the requested resource.
MalwareDetected
// NameAlreadyExists defines the error if the specified item name already exists.
NameAlreadyExists
// NotAllowed defines the error if the action is not allowed by the system.
NotAllowed
// NotSupported defines the error if the request is not supported by the system.
NotSupported
// ResourceModified defines the error if the resource being updated has changed since the caller last read it, usually an eTag mismatch.
ResourceModified
// ResyncRequired defines the error if the delta token is no longer valid, and the app must reset the sync state.
ResyncRequired
// ServiceNotAvailable defines the error if the service is not available. Try the request again after a delay. There may be a Retry-After header.
ServiceNotAvailable
// SyncStateNotFound defines the error when the sync state generation is not found. The delta token is expired and data must be synchronized again.
2023-09-08 15:40:06 +02:00
SyncStateNotFound
2019-12-09 15:54:14 +01:00
// QuotaLimitReached the user has reached their quota limit.
QuotaLimitReached
// Unauthenticated the caller is not authenticated.
Unauthenticated
2022-10-13 15:59:30 +02:00
// PreconditionFailed the request cannot be made and this error response is sent back
PreconditionFailed
2023-11-25 10:22:41 +01:00
// ItemIsLocked The item is locked by another process. Try again later.
ItemIsLocked
2019-12-09 15:54:14 +01:00
)
var errorCodes = [...]string{
"accessDenied",
"activityLimitReached",
"generalException",
2021-07-09 15:15:20 +00:00
"InvalidAuthenticationToken",
2019-12-09 15:54:14 +01:00
"invalidRange",
"invalidRequest",
"itemNotFound",
"malwareDetected",
"nameAlreadyExists",
"notAllowed",
"notSupported",
"resourceModified",
"resyncRequired",
"serviceNotAvailable",
2023-09-08 15:40:06 +02:00
"syncStateNotFound",
2019-12-09 15:54:14 +01:00
"quotaLimitReached",
"unauthenticated",
2022-10-13 15:59:30 +02:00
"preconditionFailed",
2023-11-25 10:22:41 +01:00
"itemIsLocked",
2019-12-09 15:54:14 +01:00
}
// New constructs a new errorcode.Error
func New(e ErrorCode, msg string) Error {
return Error{
errorCode: e,
msg: msg,
}
}
// Render writes a Graph ErrorCode object to the response writer
2021-07-09 15:15:20 +00:00
func (e ErrorCode) Render(w http.ResponseWriter, r *http.Request, status int, msg string) {
render.Status(r, status)
render.JSON(w, r, e.CreateOdataError(r.Context(), msg))
}
// CreateOdataError creates and populates a Graph ErrorCode object
func (e ErrorCode) CreateOdataError(ctx context.Context, msg string) *libregraph.OdataError {
innererror := map[string]interface{}{
"date": time.Now().UTC().Format(time.RFC3339),
}
2021-08-12 17:14:41 +02:00
innererror["request-id"] = middleware.GetReqID(ctx)
return &libregraph.OdataError{
2021-12-03 09:14:22 +01:00
Error: libregraph.OdataErrorMain{
2021-08-12 17:14:41 +02:00
Code: e.String(),
Message: msg,
Innererror: innererror,
},
2019-12-09 15:54:14 +01:00
}
}
// Render writes a Graph Error object to the response writer
func (e Error) Render(w http.ResponseWriter, r *http.Request) {
var status int
switch e.errorCode {
2023-09-08 15:40:06 +02:00
case AccessDenied:
status = http.StatusForbidden
case NotSupported:
status = http.StatusNotImplemented
case InvalidRange:
2023-09-08 15:40:06 +02:00
status = http.StatusRequestedRangeNotSatisfiable
case InvalidRequest:
status = http.StatusBadRequest
case ItemNotFound:
status = http.StatusNotFound
case NameAlreadyExists:
status = http.StatusConflict
case NotAllowed:
2023-09-08 15:40:06 +02:00
status = http.StatusMethodNotAllowed
2023-11-25 10:22:41 +01:00
case ItemIsLocked:
status = http.StatusLocked
case PreconditionFailed:
status = http.StatusPreconditionFailed
default:
status = http.StatusInternalServerError
}
e.errorCode.Render(w, r, status, e.msg)
}
// String returns the string corresponding to the ErrorCode
2019-12-09 15:54:14 +01:00
func (e ErrorCode) String() string {
return errorCodes[e]
}
// Error return the concatenation of the error string and optional message
func (e Error) Error() string {
errString := errorCodes[e.errorCode]
if e.msg != "" {
errString += ": " + e.msg
}
return errString
}
2023-11-28 17:13:30 +01:00
func (e Error) GetCode() ErrorCode {
return e.errorCode
}
2023-06-09 18:30:44 +02:00
// RenderError render the Graph Error based on a code or default one
func RenderError(w http.ResponseWriter, r *http.Request, err error) {
var errcode Error
if errors.As(err, &errcode) {
errcode.Render(w, r)
return
}
GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
}