@@ -2,9 +2,11 @@ package errors
2
2
3
3
import (
4
4
"errors"
5
+ "fmt"
5
6
"net/http"
6
7
"runtime"
7
8
"strconv"
9
+ "strings"
8
10
)
9
11
10
12
func newerr (e error , message string , file string , line int , etype errType ) * Error {
@@ -17,19 +19,34 @@ func newerr(e error, message string, file string, line int, etype errType) *Erro
17
19
}
18
20
}
19
21
22
+ func newerrf (e error , file string , line int , etype errType , format string , args ... interface {}) * Error {
23
+ message := fmt .Sprintf (format , args ... )
24
+ return newerr (e , message , file , line , etype )
25
+ }
26
+
20
27
// Wrap is used to simply wrap an error with no custom error message with Error struct; with the error
21
28
// type being defaulted to `TypeInternal`
22
29
// If the error being wrapped is already of type Error, then its respective type is used
23
- func Wrap (err error ) * Error {
30
+ func Wrap (err error , msg ... string ) * Error {
31
+ message := strings .Join (msg , ". " )
24
32
_ , file , line , _ := runtime .Caller (1 )
25
33
e , _ := err .(* Error )
26
34
if e == nil {
27
- return newerr (err , "" , file , line , TypeInternal )
35
+ return newerr (err , message , file , line , TypeInternal )
28
36
}
29
- return newerr (err , "" , file , line , e .eType )
37
+ return newerr (err , message , file , line , e .eType )
30
38
}
31
39
32
- // WrapWithMsg wrap error with a user friendly message
40
+ func Wrapf (err error , format string , args ... interface {}) * Error {
41
+ _ , file , line , _ := runtime .Caller (1 )
42
+ e , _ := err .(* Error )
43
+ if e == nil {
44
+ return newerrf (e , file , line , TypeInternal , format , args ... )
45
+ }
46
+ return newerrf (e , file , line , e .Type (), format , args ... )
47
+ }
48
+
49
+ // WrapWithMsg [deprecated, use `Wrap`] wrap error with a user friendly message
33
50
func WrapWithMsg (err error , msg string ) * Error {
34
51
_ , file , line , _ := runtime .Caller (1 )
35
52
e , _ := err .(* Error )
@@ -45,144 +62,289 @@ func NewWithType(msg string, etype errType) *Error {
45
62
return newerr (nil , msg , file , line , etype )
46
63
}
47
64
65
+ // NewWithTypef returns an error instance with custom error type. And formatted message
66
+ func NewWithTypef (etype errType , format string , args ... interface {}) * Error {
67
+ _ , file , line , _ := runtime .Caller (1 )
68
+ return newerrf (nil , file , line , etype , format , args ... )
69
+ }
70
+
48
71
// NewWithErrMsgType returns an error instance with custom error type and message
49
72
func NewWithErrMsgType (e error , message string , etype errType ) * Error {
50
73
_ , file , line , _ := runtime .Caller (1 )
51
74
return newerr (e , message , file , line , etype )
52
75
}
53
76
77
+ // NewWithErrMsgTypef returns an error instance with custom error type and formatted message
78
+ func NewWithErrMsgTypef (e error , etype errType , format string , args ... interface {}) * Error {
79
+ _ , file , line , _ := runtime .Caller (1 )
80
+ return newerrf (e , file , line , etype , format , args ... )
81
+ }
82
+
54
83
// Internal helper method for creating internal errors
55
84
func Internal (message string ) * Error {
56
85
_ , file , line , _ := runtime .Caller (1 )
57
86
return newerr (nil , message , file , line , TypeInternal )
58
87
}
59
88
89
+ // Internalf helper method for creating internal errors with formatted message
90
+ func Internalf (format string , args ... interface {}) * Error {
91
+ _ , file , line , _ := runtime .Caller (1 )
92
+ return newerrf (nil , file , line , TypeInternal , format , args ... )
93
+ }
94
+
60
95
// Validation is a helper function to create a new error of type TypeValidation
61
96
func Validation (message string ) * Error {
62
97
_ , file , line , _ := runtime .Caller (1 )
63
98
return newerr (nil , message , file , line , TypeValidation )
64
99
}
65
100
101
+ // Validationf is a helper function to create a new error of type TypeValidation, with formatted message
102
+ func Validationf (format string , args ... interface {}) * Error {
103
+ _ , file , line , _ := runtime .Caller (1 )
104
+ return newerrf (nil , file , line , TypeValidation , format , args ... )
105
+ }
106
+
66
107
// InputBody is a helper function to create a new error of type TypeInputBody
67
108
func InputBody (message string ) * Error {
68
109
_ , file , line , _ := runtime .Caller (1 )
69
110
return newerr (nil , message , file , line , TypeInputBody )
70
111
}
71
112
113
+ // InputBodyf is a helper function to create a new error of type TypeInputBody, with formatted message
114
+ func InputBodyf (format string , args ... interface {}) * Error {
115
+ _ , file , line , _ := runtime .Caller (1 )
116
+ return newerrf (nil , file , line , TypeInputBody , format , args ... )
117
+ }
118
+
72
119
// Duplicate is a helper function to create a new error of type TypeDuplicate
73
120
func Duplicate (message string ) * Error {
74
121
_ , file , line , _ := runtime .Caller (1 )
75
122
return newerr (nil , message , file , line , TypeDuplicate )
76
123
}
77
124
125
+ // Duplicatef is a helper function to create a new error of type TypeDuplicate, with formatted message
126
+ func Duplicatef (format string , args ... interface {}) * Error {
127
+ _ , file , line , _ := runtime .Caller (1 )
128
+ return newerrf (nil , file , line , TypeDuplicate , format , args ... )
129
+ }
130
+
78
131
// Unauthenticated is a helper function to create a new error of type TypeUnauthenticated
79
132
func Unauthenticated (message string ) * Error {
80
133
_ , file , line , _ := runtime .Caller (1 )
81
134
return newerr (nil , message , file , line , TypeUnauthenticated )
82
135
}
83
136
137
+ // Unauthenticatedf is a helper function to create a new error of type TypeUnauthenticated, with formatted message
138
+ func Unauthenticatedf (format string , args ... interface {}) * Error {
139
+ _ , file , line , _ := runtime .Caller (1 )
140
+ return newerrf (nil , file , line , TypeUnauthenticated , format , args ... )
141
+
142
+ }
143
+
84
144
// Unauthorized is a helper function to create a new error of type TypeUnauthorized
85
145
func Unauthorized (message string ) * Error {
86
146
_ , file , line , _ := runtime .Caller (1 )
87
147
return newerr (nil , message , file , line , TypeUnauthorized )
88
148
}
89
149
150
+ // Unauthorizedf is a helper function to create a new error of type TypeUnauthorized, with formatted message
151
+ func Unauthorizedf (format string , args ... interface {}) * Error {
152
+ _ , file , line , _ := runtime .Caller (1 )
153
+ return newerrf (nil , file , line , TypeUnauthorized , format , args ... )
154
+ }
155
+
90
156
// Empty is a helper function to create a new error of type TypeEmpty
91
157
func Empty (message string ) * Error {
92
158
_ , file , line , _ := runtime .Caller (1 )
93
159
return newerr (nil , message , file , line , TypeEmpty )
94
160
}
95
161
162
+ // Emptyf is a helper function to create a new error of type TypeEmpty, with formatted message
163
+ func Emptyf (format string , args ... interface {}) * Error {
164
+ _ , file , line , _ := runtime .Caller (1 )
165
+ return newerrf (nil , file , line , TypeEmpty , format , args ... )
166
+ }
167
+
96
168
// NotFound is a helper function to create a new error of type TypeNotFound
97
169
func NotFound (message string ) * Error {
98
170
_ , file , line , _ := runtime .Caller (1 )
99
171
return newerr (nil , message , file , line , TypeNotFound )
100
172
}
101
173
174
+ // NotFoundf is a helper function to create a new error of type TypeNotFound, with formatted message
175
+ func NotFoundf (format string , args ... interface {}) * Error {
176
+ _ , file , line , _ := runtime .Caller (1 )
177
+ return newerrf (nil , file , line , TypeNotFound , format , args ... )
178
+ }
179
+
102
180
// MaximumAttempts is a helper function to create a new error of type TypeMaximumAttempts
103
181
func MaximumAttempts (message string ) * Error {
104
182
_ , file , line , _ := runtime .Caller (1 )
105
183
return newerr (nil , message , file , line , TypeMaximumAttempts )
106
184
}
107
185
186
+ // MaximumAttemptsf is a helper function to create a new error of type TypeMaximumAttempts, with formatted message
187
+ func MaximumAttemptsf (format string , args ... interface {}) * Error {
188
+ _ , file , line , _ := runtime .Caller (1 )
189
+ return newerrf (nil , file , line , TypeMaximumAttempts , format , args ... )
190
+ }
191
+
108
192
// SubscriptionExpired is a helper function to create a new error of type TypeSubscriptionExpired
109
193
func SubscriptionExpired (message string ) * Error {
110
194
_ , file , line , _ := runtime .Caller (1 )
111
195
return newerr (nil , message , file , line , TypeSubscriptionExpired )
112
196
}
113
197
198
+ // SubscriptionExpiredf is a helper function to create a new error of type TypeSubscriptionExpired, with formatted message
199
+ func SubscriptionExpiredf (format string , args ... interface {}) * Error {
200
+ _ , file , line , _ := runtime .Caller (1 )
201
+ return newerrf (nil , file , line , TypeSubscriptionExpired , format , args ... )
202
+ }
203
+
114
204
// DownstreamDependencyTimedout is a helper function to create a new error of type TypeDownstreamDependencyTimedout
115
205
func DownstreamDependencyTimedout (message string ) * Error {
116
206
_ , file , line , _ := runtime .Caller (1 )
117
207
return newerr (nil , message , file , line , TypeDownstreamDependencyTimedout )
118
208
}
119
209
210
+ // DownstreamDependencyTimedoutf is a helper function to create a new error of type TypeDownstreamDependencyTimedout, with formatted message
211
+ func DownstreamDependencyTimedoutf (format string , args ... interface {}) * Error {
212
+ _ , file , line , _ := runtime .Caller (1 )
213
+ return newerrf (nil , file , line , TypeDownstreamDependencyTimedout , format , args ... )
214
+ }
215
+
120
216
// InternalErr helper method for creation internal errors which also accepts an original error
121
217
func InternalErr (original error , message string ) * Error {
122
218
_ , file , line , _ := runtime .Caller (1 )
123
219
return newerr (original , message , file , line , TypeInternal )
124
220
}
125
221
222
+ // InternalErr helper method for creation internal errors which also accepts an original error, with formatted message
223
+ func InternalErrf (original error , format string , args ... interface {}) * Error {
224
+ _ , file , line , _ := runtime .Caller (1 )
225
+ return newerrf (original , file , line , TypeInternal , format , args ... )
226
+ }
227
+
126
228
// ValidationErr helper method for creation validation errors which also accepts an original error
127
229
func ValidationErr (original error , message string ) * Error {
128
230
_ , file , line , _ := runtime .Caller (1 )
129
231
return newerr (original , message , file , line , TypeValidation )
130
232
}
131
233
234
+ // ValidationErr helper method for creation validation errors which also accepts an original error, with formatted message
235
+ func ValidationErrf (original error , format string , args ... interface {}) * Error {
236
+ _ , file , line , _ := runtime .Caller (1 )
237
+ return newerrf (original , file , line , TypeValidation , format , args ... )
238
+ }
239
+
132
240
// InputBodyErr is a helper function to create a new error of type TypeInputBody which also accepts an original error
133
241
func InputBodyErr (original error , message string ) * Error {
134
242
_ , file , line , _ := runtime .Caller (1 )
135
243
return newerr (original , message , file , line , TypeInputBody )
136
244
}
137
245
246
+ // InputBodyErrf is a helper function to create a new error of type TypeInputBody which also accepts an original error, with formatted message
247
+ func InputBodyErrf (original error , format string , args ... interface {}) * Error {
248
+ _ , file , line , _ := runtime .Caller (1 )
249
+ return newerrf (original , file , line , TypeInputBody , format , args ... )
250
+ }
251
+
138
252
// DuplicateErr is a helper function to create a new error of type TypeDuplicate which also accepts an original error
139
253
func DuplicateErr (original error , message string ) * Error {
140
254
_ , file , line , _ := runtime .Caller (1 )
141
255
return newerr (original , message , file , line , TypeDuplicate )
142
256
}
143
257
258
+ // DuplicateErrf is a helper function to create a new error of type TypeDuplicate which also accepts an original error, with formatted message
259
+ func DuplicateErrf (original error , format string , args ... interface {}) * Error {
260
+ _ , file , line , _ := runtime .Caller (1 )
261
+ return newerrf (original , file , line , TypeDuplicate , format , args ... )
262
+ }
263
+
144
264
// UnauthenticatedErr is a helper function to create a new error of type TypeUnauthenticated which also accepts an original error
145
265
func UnauthenticatedErr (original error , message string ) * Error {
146
266
_ , file , line , _ := runtime .Caller (1 )
147
267
return newerr (original , message , file , line , TypeUnauthenticated )
148
268
}
149
269
270
+ // UnauthenticatedErrf is a helper function to create a new error of type TypeUnauthenticated which also accepts an original error, with formatted message
271
+ func UnauthenticatedErrf (original error , format string , args ... interface {}) * Error {
272
+ _ , file , line , _ := runtime .Caller (1 )
273
+ return newerrf (original , file , line , TypeUnauthenticated , format , args ... )
274
+ }
275
+
150
276
// UnauthorizedErr is a helper function to create a new error of type TypeUnauthorized which also accepts an original error
151
277
func UnauthorizedErr (original error , message string ) * Error {
152
278
_ , file , line , _ := runtime .Caller (1 )
153
279
return newerr (original , message , file , line , TypeUnauthorized )
154
280
}
155
281
282
+ // UnauthorizedErrf is a helper function to create a new error of type TypeUnauthorized which also accepts an original error, with formatted message
283
+ func UnauthorizedErrf (original error , format string , args ... interface {}) * Error {
284
+ _ , file , line , _ := runtime .Caller (1 )
285
+ return newerrf (original , file , line , TypeUnauthorized , format , args ... )
286
+ }
287
+
156
288
// EmptyErr is a helper function to create a new error of type TypeEmpty which also accepts an original error
157
289
func EmptyErr (original error , message string ) * Error {
158
290
_ , file , line , _ := runtime .Caller (1 )
159
291
return newerr (original , message , file , line , TypeEmpty )
160
292
}
161
293
294
+ // EmptyErr is a helper function to create a new error of type TypeEmpty which also accepts an original error, with formatted message
295
+ func EmptyErrf (original error , format string , args ... interface {}) * Error {
296
+ _ , file , line , _ := runtime .Caller (1 )
297
+ return newerrf (original , file , line , TypeEmpty , format , args ... )
298
+ }
299
+
162
300
// NotFoundErr is a helper function to create a new error of type TypeNotFound which also accepts an original error
163
301
func NotFoundErr (original error , message string ) * Error {
164
302
_ , file , line , _ := runtime .Caller (1 )
165
303
return newerr (original , message , file , line , TypeNotFound )
166
304
}
167
305
306
+ // NotFoundErrf is a helper function to create a new error of type TypeNotFound which also accepts an original error, with formatted message
307
+ func NotFoundErrf (original error , format string , args ... interface {}) * Error {
308
+ _ , file , line , _ := runtime .Caller (1 )
309
+ return newerrf (original , file , line , TypeNotFound , format , args ... )
310
+ }
311
+
168
312
// MaximumAttemptsErr is a helper function to create a new error of type TypeMaximumAttempts which also accepts an original error
169
313
func MaximumAttemptsErr (original error , message string ) * Error {
170
314
_ , file , line , _ := runtime .Caller (1 )
171
315
return newerr (original , message , file , line , TypeMaximumAttempts )
172
316
}
173
317
318
+ // MaximumAttemptsErr is a helper function to create a new error of type TypeMaximumAttempts which also accepts an original error, with formatted message
319
+ func MaximumAttemptsErrf (original error , format string , args ... interface {}) * Error {
320
+ _ , file , line , _ := runtime .Caller (1 )
321
+ return newerrf (original , file , line , TypeMaximumAttempts , format , args ... )
322
+ }
323
+
174
324
// SubscriptionExpiredErr is a helper function to create a new error of type TypeSubscriptionExpired which also accepts an original error
175
325
func SubscriptionExpiredErr (original error , message string ) * Error {
176
326
_ , file , line , _ := runtime .Caller (1 )
177
327
return newerr (original , message , file , line , TypeSubscriptionExpired )
178
328
}
179
329
330
+ // SubscriptionExpiredErrf is a helper function to create a new error of type TypeSubscriptionExpired which also accepts an original error, with formatted message
331
+ func SubscriptionExpiredErrf (original error , format string , args ... interface {}) * Error {
332
+ _ , file , line , _ := runtime .Caller (1 )
333
+ return newerrf (original , file , line , TypeSubscriptionExpired , format , args ... )
334
+ }
335
+
180
336
// DownstreamDependencyTimedoutErr is a helper function to create a new error of type TypeDownstreamDependencyTimedout which also accepts an original error
181
337
func DownstreamDependencyTimedoutErr (original error , message string ) * Error {
182
338
_ , file , line , _ := runtime .Caller (1 )
183
339
return newerr (original , message , file , line , TypeDownstreamDependencyTimedout )
184
340
}
185
341
342
+ // DownstreamDependencyTimedoutErrf is a helper function to create a new error of type TypeDownstreamDependencyTimedout which also accepts an original error, with formatted message
343
+ func DownstreamDependencyTimedoutErrf (original error , format string , args ... interface {}) * Error {
344
+ _ , file , line , _ := runtime .Caller (1 )
345
+ return newerrf (original , file , line , TypeDownstreamDependencyTimedout , format , args ... )
346
+ }
347
+
186
348
// HTTPStatusCodeMessage returns the appropriate HTTP status code, message, boolean for the error
187
349
// the boolean value is true if the error was of type *Error, false otherwise
188
350
func HTTPStatusCodeMessage (err error ) (int , string , bool ) {
0 commit comments