/
errors.go
190 lines (151 loc) · 4.89 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package clients
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/labstack/gommon/log"
"github.com/sirupsen/logrus"
)
var (
ErrBreachingAbuseLimit error = errors.New("breaching abuse limit")
)
type ErrorType string
const (
ErrorTypeBase64Encode ErrorType = "Base64Encode"
ErrorTypeBase64Decode ErrorType = "Base64Decode"
ErrorTypeJSONMarshal ErrorType = "JSONMarshal"
ErrorTypeJSONUnmarshal ErrorType = "JSONUnmarshal"
ErrorTypeJWTExpired ErrorType = "JWTExpired"
ErrorTypeUnauthorized ErrorType = "Unauthorized"
ErrorTypePermissionDenied ErrorType = "PermissionDenied"
ErrorTypeTokenRefresh ErrorType = "TokenRefresh"
ErrorTypeInvalidResponse ErrorType = "InvalidResponse"
ErrorTypeBadResponse ErrorType = "BadResponse"
ErrorTypeBadRequest ErrorType = "BadRequest"
ErrorTypeBadGateway ErrorType = "BadGateway"
ErrorTypeRequestFailed ErrorType = "RequestFailed"
ErrorTypeDoesNotExist ErrorType = "DoesNotExist"
ErrorTypeInvalidKey ErrorType = "InvalidKey"
ErrorTypeKeyNotFound ErrorType = "KeyNotFound"
ErrorTypeRecordNotFound ErrorType = "RecordNotFound"
ErrorTypeInvalidToken ErrorType = "InvalidToken"
ErrorTypeInvalidAccountConfiguration ErrorType = "InvalidAccountConfiguration"
ErrorTypeInvalidProjectConfiguration ErrorType = "InvalidProjectConfiguration"
ErrorTypeEmailFailed ErrorType = "EmailFailed"
)
var ResponseCodeMap = map[ErrorType]int{
ErrorTypeJSONMarshal: http.StatusBadRequest,
ErrorTypeJSONUnmarshal: http.StatusBadRequest,
ErrorTypeJWTExpired: http.StatusUnauthorized,
ErrorTypeUnauthorized: http.StatusUnauthorized,
ErrorTypePermissionDenied: http.StatusForbidden,
ErrorTypeTokenRefresh: http.StatusConflict,
ErrorTypeInvalidResponse: http.StatusInternalServerError,
ErrorTypeBadResponse: http.StatusInternalServerError,
ErrorTypeBadRequest: http.StatusBadRequest,
ErrorTypeBadGateway: http.StatusBadGateway,
ErrorTypeRequestFailed: http.StatusBadRequest,
ErrorTypeDoesNotExist: http.StatusNotFound,
ErrorTypeInvalidKey: http.StatusBadRequest,
ErrorTypeKeyNotFound: http.StatusNotFound,
ErrorTypeRecordNotFound: http.StatusNotFound,
ErrorTypeInvalidAccountConfiguration: http.StatusBadRequest,
ErrorTypeInvalidProjectConfiguration: http.StatusBadRequest,
ErrorTypeInvalidToken: http.StatusBadRequest,
ErrorTypeEmailFailed: http.StatusInternalServerError,
}
func (e *ErrorType) GetStatusCode() int {
for key, value := range ResponseCodeMap {
if &key == e {
return value
}
}
return http.StatusBadRequest
}
type ErrorSource string
const (
ErrorSourceHTTP ErrorSource = "http"
ErrorSourceGraphQL ErrorSource = "graphql"
ErrorSourceHermes ErrorSource = "hermes"
ErrorSourceMailer ErrorSource = "mailer"
ErrorSourceVault ErrorSource = "vault"
ErrorSourceNhost ErrorSource = "nhost"
ErrorSourceGo ErrorSource = "go"
ErrorSourceSystem ErrorSource = "system"
ErrorSourceGithub ErrorSource = "github"
ErrorSourceVercel ErrorSource = "vercel"
)
type Error struct {
Error error `json:"error"`
Message string `json:"message"`
Type ErrorType `json:"type"`
Source ErrorSource `json:"source"`
}
func (e *Error) ToError() error {
result, _ := json.Marshal(e)
return fmt.Errorf(string(result))
}
func New(err error, message string, typ ErrorType, source ErrorSource) *Error {
return &Error{
Error: err,
Message: message,
Type: typ,
Source: source,
}
}
func Parse(err error) *Error {
var result Error
_ = json.Unmarshal([]byte(err.Error()), &result)
return &result
}
func ParseExternal(err error) *Error {
payload := strings.Split(err.Error(), ":")
var response Error
response.Error = err
switch strings.TrimSpace(payload[0]) {
case "graphql":
response.Source = ErrorSourceGraphQL
}
response.Message = strings.TrimSpace(payload[1])
if strings.Contains(response.Message, "permission has failed") {
response.Type = ErrorTypePermissionDenied
response.Message = response.GenerateMessage("Permission Denied")
}
if len(payload) > 2 {
switch strings.TrimSpace(payload[2]) {
case "JWTExpired":
response.Type = ErrorTypeJWTExpired
}
}
return &response
}
func (e *Error) IsType(errType ErrorType) bool {
return e.Type == errType
}
func (e *Error) GenerateMessage(defaultMessage string) string {
switch e.Type {
case ErrorTypePermissionDenied:
return "You do not have permissions to perform this action"
default:
return defaultMessage
}
}
func (e *Error) Log(logger *logrus.Logger, defaultMessage string) {
level := logger.GetLevel()
if level == logrus.DebugLevel {
log.Debug(e.Error)
}
switch e.Type {
case ErrorTypeUnauthorized:
logger.Error("you are not authorized to perform this action")
}
if defaultMessage != "" {
if level == logrus.ErrorLevel {
logger.Error(defaultMessage)
} else {
logger.Fatal(defaultMessage)
}
}
}