mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-03-27 20:01:48 -04:00
enhancement: add error origin information to the errorcode package
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
cs3rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
|
||||
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
|
||||
|
||||
"github.com/cs3org/reva/v2/pkg/utils"
|
||||
)
|
||||
|
||||
@@ -19,11 +20,12 @@ import (
|
||||
// This function is particularly useful when dealing with CS3 responses,
|
||||
// and a unified error handling within the application is necessary.
|
||||
func FromCS3Status(status *cs3rpc.Status, inerr error, ignore ...cs3rpc.Code) error {
|
||||
if inerr != nil {
|
||||
return Error{msg: inerr.Error(), errorCode: GeneralException}
|
||||
}
|
||||
err := Error{errorCode: GeneralException, msg: "unspecified error has occurred", origin: ErrorOriginCS3}
|
||||
|
||||
err := Error{errorCode: GeneralException, msg: "unspecified error has occurred"}
|
||||
if inerr != nil {
|
||||
err.msg = inerr.Error()
|
||||
return err
|
||||
}
|
||||
|
||||
if status != nil {
|
||||
err.msg = status.GetMessage()
|
||||
|
||||
@@ -44,6 +44,11 @@ func TestFromCS3Status(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
var e errorcode.Error
|
||||
if errors.As(test.expected, &e) {
|
||||
test.expected = e.WithOrigin(errorcode.ErrorOriginCS3)
|
||||
}
|
||||
|
||||
if got := errorcode.FromCS3Status(test.status, test.err, test.ignore...); !reflect.DeepEqual(got, test.expected) {
|
||||
t.Error("Test Failed: {} expected, received: {}", test.expected, got)
|
||||
}
|
||||
@@ -56,7 +61,7 @@ func TestFromStat(t *testing.T) {
|
||||
err error
|
||||
result error
|
||||
}{
|
||||
{nil, errors.New("some error"), errorcode.New(errorcode.GeneralException, "some error")},
|
||||
{nil, errors.New("some error"), errorcode.New(errorcode.GeneralException, "some error").WithOrigin(errorcode.ErrorOriginCS3)},
|
||||
{&provider.StatResponse{Status: &cs3rpc.Status{Code: cs3rpc.Code_CODE_OK}}, nil, nil},
|
||||
}
|
||||
|
||||
|
||||
@@ -12,15 +12,28 @@ import (
|
||||
libregraph "github.com/owncloud/libre-graph-api-go"
|
||||
)
|
||||
|
||||
// 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
|
||||
origin ErrorOrigin
|
||||
}
|
||||
|
||||
// ErrorOrigin gives information about where the error originated
|
||||
type ErrorOrigin int
|
||||
|
||||
const (
|
||||
// ErrorOriginUnknown is the default error source
|
||||
// and indicates that the error does not have any information about its origin
|
||||
ErrorOriginUnknown ErrorOrigin = iota
|
||||
|
||||
// ErrorOriginCS3 indicates that the error originated from a CS3 service
|
||||
ErrorOriginCS3
|
||||
)
|
||||
|
||||
// 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
|
||||
|
||||
// List taken from https://github.com/microsoft/microsoft-graph-docs-1/blob/main/concepts/errors.md#code-property
|
||||
const (
|
||||
// AccessDenied defines the error if the caller doesn't have permission to perform the action.
|
||||
@@ -148,7 +161,7 @@ func (e ErrorCode) String() string {
|
||||
return errorCodes[e]
|
||||
}
|
||||
|
||||
// Error return the concatenation of the error string and optional message
|
||||
// Error returns the concatenation of the error string and optional message
|
||||
func (e Error) Error() string {
|
||||
errString := errorCodes[e.errorCode]
|
||||
if e.msg != "" {
|
||||
@@ -161,13 +174,35 @@ func (e Error) GetCode() ErrorCode {
|
||||
return e.errorCode
|
||||
}
|
||||
|
||||
// GetOrigin returns the source of the error
|
||||
func (e Error) GetOrigin() ErrorOrigin {
|
||||
return e.origin
|
||||
}
|
||||
|
||||
// WithOrigin returns a new Error with the provided origin
|
||||
func (e Error) WithOrigin(o ErrorOrigin) Error {
|
||||
e.origin = o
|
||||
return e
|
||||
}
|
||||
|
||||
// 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)
|
||||
e, ok := ToError(err)
|
||||
if !ok {
|
||||
GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
GeneralException.Render(w, r, http.StatusInternalServerError, err.Error())
|
||||
e.Render(w, r)
|
||||
}
|
||||
|
||||
// ToError checks if the error is of type Error and returns it,
|
||||
// the second parameter indicates if the error conversion was successful
|
||||
func ToError(err error) (Error, bool) {
|
||||
var e Error
|
||||
if errors.As(err, &e) {
|
||||
return e, true
|
||||
}
|
||||
|
||||
return Error{}, false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user