-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
116 lines (93 loc) · 2.46 KB
/
errors.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package client
import (
"errors"
"fmt"
"github.com/hasura/go-graphql-client"
)
var (
ErrGraphqlIDIsEmpty = errors.New("id is empty")
ErrGraphqlNameIsEmpty = errors.New("name is empty")
ErrGraphqlEmptyBothNameAndID = errors.New("both name and id should not be empty")
ErrGraphqlResultIsEmpty = errors.New("query result is empty")
ErrGraphqlConnectorIDIsEmpty = errors.New("connector id is empty")
ErrGraphqlNetworkIDIsEmpty = errors.New("network id is empty")
ErrGraphqlNetworkNameIsEmpty = errors.New("network name is empty")
ErrGraphqlEmailIsEmpty = errors.New("email is empty")
)
type HTTPError struct {
RequestURI string
StatusCode int
Body []byte
}
func NewHTTPError(requestURI string, statusCode int, body []byte) *HTTPError {
return &HTTPError{
RequestURI: requestURI,
StatusCode: statusCode,
Body: body,
}
}
func (e *HTTPError) Error() string {
return fmt.Sprintf("request %s failed, status %d, body %s", e.RequestURI, e.StatusCode, e.Body)
}
type APIError struct {
WrappedError error
Operation string
Resource string
ID graphql.ID
Name string
}
func NewAPIErrorWithID(wrappedError error, operation, resource, id string) *APIError {
return &APIError{
WrappedError: wrappedError,
Operation: operation,
Resource: resource,
ID: graphql.ID(id),
}
}
func NewAPIErrorWithName(wrappedError error, operation, resource, name string) *APIError {
return &APIError{
WrappedError: wrappedError,
Operation: operation,
Resource: resource,
Name: name,
}
}
func NewAPIError(wrappedError error, operation, resource string) *APIError {
return &APIError{
WrappedError: wrappedError,
Operation: operation,
Resource: resource,
}
}
func (e *APIError) Error() string {
var args = make([]interface{}, 0, 2) //nolint:gomnd
args = append(args, e.Operation, e.Resource)
var format = "failed to %s %s"
if e.ID != "" {
format += " with id %s"
args = append(args, e.ID)
}
if e.Name != "" {
format += " with name %s"
args = append(args, e.Name)
}
if e.WrappedError != nil {
format += ": %s"
args = append(args, e.WrappedError)
}
return fmt.Sprintf(format, args...)
}
func (e *APIError) Unwrap() error {
return e.WrappedError
}
type MutationError struct {
Message string
}
func NewMutationError(message string) *MutationError {
return &MutationError{
Message: message,
}
}
func (e *MutationError) Error() string {
return e.Message
}