Skip to content

Commit d9a4fa5

Browse files
committed
[minor] added support for formatted message for all helper methods, yay! (this was delayed far too long)
1 parent e43b460 commit d9a4fa5

File tree

2 files changed

+222
-4
lines changed

2 files changed

+222
-4
lines changed

helper.go

Lines changed: 166 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package errors
22

33
import (
44
"errors"
5+
"fmt"
56
"net/http"
67
"runtime"
78
"strconv"
9+
"strings"
810
)
911

1012
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
1719
}
1820
}
1921

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+
2027
// Wrap is used to simply wrap an error with no custom error message with Error struct; with the error
2128
// type being defaulted to `TypeInternal`
2229
// 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, ". ")
2432
_, file, line, _ := runtime.Caller(1)
2533
e, _ := err.(*Error)
2634
if e == nil {
27-
return newerr(err, "", file, line, TypeInternal)
35+
return newerr(err, message, file, line, TypeInternal)
2836
}
29-
return newerr(err, "", file, line, e.eType)
37+
return newerr(err, message, file, line, e.eType)
3038
}
3139

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
3350
func WrapWithMsg(err error, msg string) *Error {
3451
_, file, line, _ := runtime.Caller(1)
3552
e, _ := err.(*Error)
@@ -45,144 +62,289 @@ func NewWithType(msg string, etype errType) *Error {
4562
return newerr(nil, msg, file, line, etype)
4663
}
4764

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+
4871
// NewWithErrMsgType returns an error instance with custom error type and message
4972
func NewWithErrMsgType(e error, message string, etype errType) *Error {
5073
_, file, line, _ := runtime.Caller(1)
5174
return newerr(e, message, file, line, etype)
5275
}
5376

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+
5483
// Internal helper method for creating internal errors
5584
func Internal(message string) *Error {
5685
_, file, line, _ := runtime.Caller(1)
5786
return newerr(nil, message, file, line, TypeInternal)
5887
}
5988

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+
6095
// Validation is a helper function to create a new error of type TypeValidation
6196
func Validation(message string) *Error {
6297
_, file, line, _ := runtime.Caller(1)
6398
return newerr(nil, message, file, line, TypeValidation)
6499
}
65100

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+
66107
// InputBody is a helper function to create a new error of type TypeInputBody
67108
func InputBody(message string) *Error {
68109
_, file, line, _ := runtime.Caller(1)
69110
return newerr(nil, message, file, line, TypeInputBody)
70111
}
71112

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+
72119
// Duplicate is a helper function to create a new error of type TypeDuplicate
73120
func Duplicate(message string) *Error {
74121
_, file, line, _ := runtime.Caller(1)
75122
return newerr(nil, message, file, line, TypeDuplicate)
76123
}
77124

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+
78131
// Unauthenticated is a helper function to create a new error of type TypeUnauthenticated
79132
func Unauthenticated(message string) *Error {
80133
_, file, line, _ := runtime.Caller(1)
81134
return newerr(nil, message, file, line, TypeUnauthenticated)
82135
}
83136

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+
84144
// Unauthorized is a helper function to create a new error of type TypeUnauthorized
85145
func Unauthorized(message string) *Error {
86146
_, file, line, _ := runtime.Caller(1)
87147
return newerr(nil, message, file, line, TypeUnauthorized)
88148
}
89149

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+
90156
// Empty is a helper function to create a new error of type TypeEmpty
91157
func Empty(message string) *Error {
92158
_, file, line, _ := runtime.Caller(1)
93159
return newerr(nil, message, file, line, TypeEmpty)
94160
}
95161

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+
96168
// NotFound is a helper function to create a new error of type TypeNotFound
97169
func NotFound(message string) *Error {
98170
_, file, line, _ := runtime.Caller(1)
99171
return newerr(nil, message, file, line, TypeNotFound)
100172
}
101173

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+
102180
// MaximumAttempts is a helper function to create a new error of type TypeMaximumAttempts
103181
func MaximumAttempts(message string) *Error {
104182
_, file, line, _ := runtime.Caller(1)
105183
return newerr(nil, message, file, line, TypeMaximumAttempts)
106184
}
107185

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+
108192
// SubscriptionExpired is a helper function to create a new error of type TypeSubscriptionExpired
109193
func SubscriptionExpired(message string) *Error {
110194
_, file, line, _ := runtime.Caller(1)
111195
return newerr(nil, message, file, line, TypeSubscriptionExpired)
112196
}
113197

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+
114204
// DownstreamDependencyTimedout is a helper function to create a new error of type TypeDownstreamDependencyTimedout
115205
func DownstreamDependencyTimedout(message string) *Error {
116206
_, file, line, _ := runtime.Caller(1)
117207
return newerr(nil, message, file, line, TypeDownstreamDependencyTimedout)
118208
}
119209

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+
120216
// InternalErr helper method for creation internal errors which also accepts an original error
121217
func InternalErr(original error, message string) *Error {
122218
_, file, line, _ := runtime.Caller(1)
123219
return newerr(original, message, file, line, TypeInternal)
124220
}
125221

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+
126228
// ValidationErr helper method for creation validation errors which also accepts an original error
127229
func ValidationErr(original error, message string) *Error {
128230
_, file, line, _ := runtime.Caller(1)
129231
return newerr(original, message, file, line, TypeValidation)
130232
}
131233

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+
132240
// InputBodyErr is a helper function to create a new error of type TypeInputBody which also accepts an original error
133241
func InputBodyErr(original error, message string) *Error {
134242
_, file, line, _ := runtime.Caller(1)
135243
return newerr(original, message, file, line, TypeInputBody)
136244
}
137245

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+
138252
// DuplicateErr is a helper function to create a new error of type TypeDuplicate which also accepts an original error
139253
func DuplicateErr(original error, message string) *Error {
140254
_, file, line, _ := runtime.Caller(1)
141255
return newerr(original, message, file, line, TypeDuplicate)
142256
}
143257

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+
144264
// UnauthenticatedErr is a helper function to create a new error of type TypeUnauthenticated which also accepts an original error
145265
func UnauthenticatedErr(original error, message string) *Error {
146266
_, file, line, _ := runtime.Caller(1)
147267
return newerr(original, message, file, line, TypeUnauthenticated)
148268
}
149269

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+
150276
// UnauthorizedErr is a helper function to create a new error of type TypeUnauthorized which also accepts an original error
151277
func UnauthorizedErr(original error, message string) *Error {
152278
_, file, line, _ := runtime.Caller(1)
153279
return newerr(original, message, file, line, TypeUnauthorized)
154280
}
155281

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+
156288
// EmptyErr is a helper function to create a new error of type TypeEmpty which also accepts an original error
157289
func EmptyErr(original error, message string) *Error {
158290
_, file, line, _ := runtime.Caller(1)
159291
return newerr(original, message, file, line, TypeEmpty)
160292
}
161293

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+
162300
// NotFoundErr is a helper function to create a new error of type TypeNotFound which also accepts an original error
163301
func NotFoundErr(original error, message string) *Error {
164302
_, file, line, _ := runtime.Caller(1)
165303
return newerr(original, message, file, line, TypeNotFound)
166304
}
167305

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+
168312
// MaximumAttemptsErr is a helper function to create a new error of type TypeMaximumAttempts which also accepts an original error
169313
func MaximumAttemptsErr(original error, message string) *Error {
170314
_, file, line, _ := runtime.Caller(1)
171315
return newerr(original, message, file, line, TypeMaximumAttempts)
172316
}
173317

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+
174324
// SubscriptionExpiredErr is a helper function to create a new error of type TypeSubscriptionExpired which also accepts an original error
175325
func SubscriptionExpiredErr(original error, message string) *Error {
176326
_, file, line, _ := runtime.Caller(1)
177327
return newerr(original, message, file, line, TypeSubscriptionExpired)
178328
}
179329

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+
180336
// DownstreamDependencyTimedoutErr is a helper function to create a new error of type TypeDownstreamDependencyTimedout which also accepts an original error
181337
func DownstreamDependencyTimedoutErr(original error, message string) *Error {
182338
_, file, line, _ := runtime.Caller(1)
183339
return newerr(original, message, file, line, TypeDownstreamDependencyTimedout)
184340
}
185341

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+
186348
// HTTPStatusCodeMessage returns the appropriate HTTP status code, message, boolean for the error
187349
// the boolean value is true if the error was of type *Error, false otherwise
188350
func HTTPStatusCodeMessage(err error) (int, string, bool) {

0 commit comments

Comments
 (0)