implemented extended gql errors

This commit is contained in:
Djeeberjr 2021-09-18 23:50:00 +02:00
parent 20f95b8ea4
commit 024948b8ff
2 changed files with 27 additions and 2 deletions

25
internal/errors.go Normal file
View File

@ -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,
}
}

View File

@ -114,12 +114,12 @@ func isAuth(ctx context.Context) (bool, error) {
token, ok := ctx.Value("jwt").(*jwt.Token) token, ok := ctx.Value("jwt").(*jwt.Token)
if !ok { if !ok {
return false, fmt.Errorf("Unauthorized") return false, extendError("UNAUTHORIZED", "Unauthorized")
} }
if token.Valid { if token.Valid {
return true, nil return true, nil
} else { } else {
return false, fmt.Errorf("Unauthorized") return false, extendError("UNAUTHORIZED", "Unauthorized")
} }
} }