graph/errocode: Rework FromCS3Status

The handling of 'error' has been moved from FromStat() to FromCS3Status().
It's generally useful for other users of FromCS3Status()
This commit is contained in:
Ralf Haferkamp
2023-11-29 11:23:15 +01:00
committed by Ralf Haferkamp
parent faf0e242cd
commit 9d3523e310
3 changed files with 39 additions and 41 deletions

View File

@@ -7,15 +7,21 @@ import (
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
)
// FromCS3Status converts a CS3 status code into a corresponding local Error representation.
// FromCS3Status converts a CS3 status code and an error into a corresponding local Error representation.
//
// It evaluates the provided CS3 status code and returns an equivalent graph Error.
// It takes a *cs3rpc.Status, an error, and a variadic parameter of type cs3rpc.Code.
// If the error is not nil, it creates an Error object with the error message and a GeneralException code.
// If the error is nil, it evaluates the provided CS3 status code and returns an equivalent graph Error.
// If the CS3 status code does not have a direct equivalent within the app,
// or is ignored, a general purpose Error is returned.
//
// 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, ignore ...cs3rpc.Code) *Error {
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"}
if status != nil {
@@ -58,13 +64,8 @@ func FromCS3Status(status *cs3rpc.Status, ignore ...cs3rpc.Code) *Error {
// FromStat transforms a *provider.StatResponse object and an error into an *Error.
//
// It takes a stat of type *provider.StatResponse, an error, and a variadic parameter of type cs3rpc.Code.
// If the error is not nil, it creates an Error object with the error message and a GeneralException code.
// If the error is nil, it invokes the FromCS3Status function with the StatResponse Status and the ignore codes.
// It invokes the FromCS3Status function with the StatResponse Status and the ignore codes.
func FromStat(stat *provider.StatResponse, err error, ignore ...cs3rpc.Code) *Error {
switch {
case err != nil:
return &Error{msg: err.Error(), errorCode: GeneralException}
default:
return FromCS3Status(stat.GetStatus(), ignore...)
}
// TODO: look into ResourceInfo to get the postprocessing state and map that to 425 status?
return FromCS3Status(stat.GetStatus(), err, ignore...)
}