From 024948b8ffb36f52e3cde114ce65090b9653a0de Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sat, 18 Sep 2021 23:50:00 +0200 Subject: [PATCH] implemented extended gql errors --- internal/errors.go | 25 +++++++++++++++++++++++++ internal/helper.go | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 internal/errors.go diff --git a/internal/errors.go b/internal/errors.go new file mode 100644 index 0000000..1265c44 --- /dev/null +++ b/internal/errors.go @@ -0,0 +1,25 @@ +package s3browser + +import "fmt" + +type extendedError struct { + Message string + Code string +} + +func (err *extendedError) Error() string { + return err.Message +} + +func (err *extendedError) Extensions() map[string]interface{} { + return map[string]interface{}{ + "code": err.Code, + } +} + +func extendError(code, format string, a ...interface{}) *extendedError { + return &extendedError{ + Message: fmt.Sprintf(format, a...), + Code: code, + } +} diff --git a/internal/helper.go b/internal/helper.go index 5cc8f38..24f717b 100644 --- a/internal/helper.go +++ b/internal/helper.go @@ -114,12 +114,12 @@ func isAuth(ctx context.Context) (bool, error) { token, ok := ctx.Value("jwt").(*jwt.Token) if !ok { - return false, fmt.Errorf("Unauthorized") + return false, extendError("UNAUTHORIZED", "Unauthorized") } if token.Valid { return true, nil } else { - return false, fmt.Errorf("Unauthorized") + return false, extendError("UNAUTHORIZED", "Unauthorized") } }