~bigbes/core-go

ref: bbb9e45c5d637da2c4df9e5d423e234f570222c6 core-go/errors/errors.go -rw-r--r-- 2.0 KiB
bbb9e45c — Conrad Hoffmann server: support multiple bind addresses 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package errors

import (
	"errors"
	"fmt"

	"github.com/vektah/gqlparser/v2/gqlerror"
)

type ErrorCode string

// Creates a new GraphQL error with a standard error code.
func New(code ErrorCode, message string) *gqlerror.Error {
	return &gqlerror.Error{
		Message:    message,
		Extensions: map[string]any{"code": code},
	}
}

// Creates a new GraphQL error with a standard error code.
func Errorf(code ErrorCode, format string, a ...any) *gqlerror.Error {
	return &gqlerror.Error{
		Message:    fmt.Sprintf(format, a...),
		Extensions: map[string]any{"code": code},
	}
}

// Sets the field name that caused the error
func Field(err *gqlerror.Error, field string) *gqlerror.Error {
	err.Extensions["field"] = field
	return err
}

// Returns true if the first GraphQL error has the same error code as the
// reference error. This should be used, for example, to test an error from
// client.Do against an error initialized by this module (e.g.
// ErrAccessDenied). These errors do not work with the Go standard library's
// errors.Is function, nor with ==, thus this function.
func Is(err error, ref *gqlerror.Error) bool {
	var gqlerr *gqlerror.Error
	if !errors.As(err, &gqlerr) {
		return false
	}
	if code, ok := gqlerr.Extensions["code"]; ok {
		if refCode, ok := ref.Extensions["code"]; ok {
			return code == refCode
		}
	}
	return false
}

// Error codes as string constants
var (
	AccessDenied  ErrorCode = "ERR_ACCESS_DENIED"
	NotFound      ErrorCode = "ERR_NOT_FOUND"
	Unsupported   ErrorCode = "ERR_UNSUPPORTED"
	Unauthorized  ErrorCode = "ERR_UNAUTHORIZED"
	InternalError ErrorCode = "ERR_INTERNAL"
	Timeout       ErrorCode = "ERR_TIMEOUT"
)

// Error codes as Go errors
var (
	ErrAccessDenied  = New(AccessDenied, "Access denied")
	ErrNotFound      = New(NotFound, "Resource not found")
	ErrUnsupported   = New(Unsupported, "Not supported")
	ErrUnauthorized  = New(Unauthorized, "Unauthorized")
	ErrInternalError = New(InternalError, "Internal server error")
	ErrTimeout       = New(Timeout, "Operation timed out")
)