-
Notifications
You must be signed in to change notification settings - Fork 5
/
helper.go
236 lines (199 loc) · 8.4 KB
/
helper.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package errors
import (
"errors"
"net/http"
"runtime"
"strconv"
)
func newerr(e error, message string, file string, line int, etype errType) *Error {
return &Error{
original: e,
message: message,
eType: etype,
// '+' concatenation is ~100x faster than using fmt.Sprintf("%s:%d", file, line)
fileLine: file + ":" + strconv.Itoa(line),
}
}
// Wrap is used to simply wrap an error with no custom error message with Error struct; with the error
// type being defaulted to `TypeInternal`
// If the error being wrapped is already of type Error, then its respective type is used
func Wrap(err error) *Error {
_, file, line, _ := runtime.Caller(1)
e, _ := err.(*Error)
if e == nil {
return newerr(err, "", file, line, TypeInternal)
}
return newerr(err, "", file, line, e.eType)
}
// WrapWithMsg wrap error with a user friendly message
func WrapWithMsg(err error, msg string) *Error {
_, file, line, _ := runtime.Caller(1)
e, _ := err.(*Error)
if e == nil {
return newerr(err, msg, file, line, TypeInternal)
}
return newerr(err, msg, file, line, e.eType)
}
// NewWithType returns an error instance with custom error type
func NewWithType(msg string, etype errType) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, msg, file, line, etype)
}
// NewWithErrMsgType returns an error instance with custom error type and message
func NewWithErrMsgType(e error, message string, etype errType) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(e, message, file, line, etype)
}
// Internal helper method for creating internal errors
func Internal(message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, message, file, line, TypeInternal)
}
// Validation is a helper function to create a new error of type TypeValidation
func Validation(message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, message, file, line, TypeValidation)
}
// InputBody is a helper function to create a new error of type TypeInputBody
func InputBody(message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, message, file, line, TypeInputBody)
}
// Duplicate is a helper function to create a new error of type TypeDuplicate
func Duplicate(message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, message, file, line, TypeDuplicate)
}
// Unauthenticated is a helper function to create a new error of type TypeUnauthenticated
func Unauthenticated(message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, message, file, line, TypeUnauthenticated)
}
// Unauthorized is a helper function to create a new error of type TypeUnauthorized
func Unauthorized(message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, message, file, line, TypeUnauthorized)
}
// Empty is a helper function to create a new error of type TypeEmpty
func Empty(message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, message, file, line, TypeEmpty)
}
// NotFound is a helper function to create a new error of type TypeNotFound
func NotFound(message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, message, file, line, TypeNotFound)
}
// MaximumAttempts is a helper function to create a new error of type TypeMaximumAttempts
func MaximumAttempts(message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, message, file, line, TypeMaximumAttempts)
}
// SubscriptionExpired is a helper function to create a new error of type TypeSubscriptionExpired
func SubscriptionExpired(message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, message, file, line, TypeSubscriptionExpired)
}
// DownstreamDependencyTimedout is a helper function to create a new error of type TypeDownstreamDependencyTimedout
func DownstreamDependencyTimedout(message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(nil, message, file, line, TypeDownstreamDependencyTimedout)
}
// InternalErr helper method for creation internal errors which also accepts an original error
func InternalErr(original error, message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(original, message, file, line, TypeInternal)
}
// ValidationErr helper method for creation validation errors which also accepts an original error
func ValidationErr(original error, message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(original, message, file, line, TypeValidation)
}
// InputBodyErr is a helper function to create a new error of type TypeInputBody which also accepts an original error
func InputBodyErr(original error, message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(original, message, file, line, TypeInputBody)
}
// DuplicateErr is a helper function to create a new error of type TypeDuplicate which also accepts an original error
func DuplicateErr(original error, message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(original, message, file, line, TypeDuplicate)
}
// UnauthenticatedErr is a helper function to create a new error of type TypeUnauthenticated which also accepts an original error
func UnauthenticatedErr(original error, message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(original, message, file, line, TypeUnauthenticated)
}
// UnauthorizedErr is a helper function to create a new error of type TypeUnauthorized which also accepts an original error
func UnauthorizedErr(original error, message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(original, message, file, line, TypeUnauthorized)
}
// EmptyErr is a helper function to create a new error of type TypeEmpty which also accepts an original error
func EmptyErr(original error, message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(original, message, file, line, TypeEmpty)
}
// NotFoundErr is a helper function to create a new error of type TypeNotFound which also accepts an original error
func NotFoundErr(original error, message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(original, message, file, line, TypeNotFound)
}
// MaximumAttemptsErr is a helper function to create a new error of type TypeMaximumAttempts which also accepts an original error
func MaximumAttemptsErr(original error, message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(original, message, file, line, TypeMaximumAttempts)
}
// SubscriptionExpiredErr is a helper function to create a new error of type TypeSubscriptionExpired which also accepts an original error
func SubscriptionExpiredErr(original error, message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(original, message, file, line, TypeSubscriptionExpired)
}
// DownstreamDependencyTimedoutErr is a helper function to create a new error of type TypeDownstreamDependencyTimedout which also accepts an original error
func DownstreamDependencyTimedoutErr(original error, message string) *Error {
_, file, line, _ := runtime.Caller(1)
return newerr(original, message, file, line, TypeDownstreamDependencyTimedout)
}
// HTTPStatusCodeMessage returns the appropriate HTTP status code, message, boolean for the error
// the boolean value is true if the error was of type *Error, false otherwise
func HTTPStatusCodeMessage(err error) (int, string, bool) {
derr, _ := err.(*Error)
if derr != nil {
return derr.HTTPStatusCode(), derr.Message(), true
}
return http.StatusInternalServerError, err.Error(), false
}
// WriteHTTP is a convenience method which will check if the error is of type *Error and
// respond appropriately
func WriteHTTP(err error, w http.ResponseWriter) {
// INFO: consider sending back "unknown server error" message
status, msg, _ := HTTPStatusCodeMessage(err)
w.WriteHeader(status)
w.Write([]byte(msg))
}
// Type returns the errType if it's an instance of *Error, -1 otherwise
func Type(err error) errType {
e, ok := err.(*Error)
if !ok {
return errType(-1)
}
return e.Type()
}
// Type returns the errType as integer if it's an instance of *Error, -1 otherwise
func TypeInt(err error) int {
return Type(err).Int()
}
// HasType will check if the provided err type is available anywhere nested in the error
func HasType(err error, et errType) bool {
if err == nil {
return false
}
e, _ := err.(*Error)
if e == nil {
return HasType(errors.Unwrap(err), et)
}
if e.Type() == et {
return true
}
return HasType(e.Unwrap(), et)
}