4
4
package errors
5
5
6
6
import (
7
- "errors"
8
7
stderror "errors"
9
8
"fmt"
10
- "strings"
11
9
)
12
10
13
11
// a ConstError is a prototype for a certain type of error
@@ -86,12 +84,17 @@ func wrapErrorWithMsg(err error, msg string) error {
86
84
return fmt .Errorf ("%s: %w" , msg , err )
87
85
}
88
86
89
- func makeWrappedConstError (err error , format string , args ... interface {}) error {
90
- separator := " "
91
- if err .Error () == "" || errors .Is (err , & fmtNoop {}) {
92
- separator = ""
87
+ // fmtErrWithType returns an error with the provided error type and format.
88
+ func fmtErrWithType (errType ConstError , hideSuffix bool , format string , args ... interface {}) error {
89
+ msg := fmt .Sprintf (format , args ... )
90
+ if ! hideSuffix {
91
+ msg += " " + errType .Error ()
92
+ }
93
+
94
+ return & errWithType {
95
+ error : New (msg ),
96
+ errType : errType ,
93
97
}
94
- return fmt .Errorf (strings .Join ([]string {format , "%w" }, separator ), append (args , err )... )
95
98
}
96
99
97
100
// WithType is responsible for annotating an already existing error so that it
@@ -116,7 +119,7 @@ func WithType(err error, errType ConstError) error {
116
119
// interface.
117
120
func Timeoutf (format string , args ... interface {}) error {
118
121
return newLocationError (
119
- makeWrappedConstError (Timeout , format , args ... ),
122
+ fmtErrWithType (Timeout , false , format , args ... ),
120
123
1 ,
121
124
)
122
125
}
@@ -140,7 +143,7 @@ func IsTimeout(err error) bool {
140
143
// Locationer interface.
141
144
func NotFoundf (format string , args ... interface {}) error {
142
145
return newLocationError (
143
- makeWrappedConstError (NotFound , format , args ... ),
146
+ fmtErrWithType (NotFound , false , format , args ... ),
144
147
1 ,
145
148
)
146
149
}
@@ -164,7 +167,7 @@ func IsNotFound(err error) bool {
164
167
// Locationer interface.
165
168
func UserNotFoundf (format string , args ... interface {}) error {
166
169
return newLocationError (
167
- makeWrappedConstError (UserNotFound , format , args ... ),
170
+ fmtErrWithType (UserNotFound , false , format , args ... ),
168
171
1 ,
169
172
)
170
173
}
@@ -188,7 +191,7 @@ func IsUserNotFound(err error) bool {
188
191
// the Locationer interface.
189
192
func Unauthorizedf (format string , args ... interface {}) error {
190
193
return newLocationError (
191
- makeWrappedConstError ( Hide ( Unauthorized ) , format , args ... ),
194
+ fmtErrWithType ( Unauthorized , true , format , args ... ),
192
195
1 ,
193
196
)
194
197
}
@@ -212,7 +215,7 @@ func IsUnauthorized(err error) bool {
212
215
// the Locationer interface.
213
216
func NotImplementedf (format string , args ... interface {}) error {
214
217
return newLocationError (
215
- makeWrappedConstError (NotImplemented , format , args ... ),
218
+ fmtErrWithType (NotImplemented , false , format , args ... ),
216
219
1 ,
217
220
)
218
221
}
@@ -236,7 +239,7 @@ func IsNotImplemented(err error) bool {
236
239
// the Locationer interface.
237
240
func AlreadyExistsf (format string , args ... interface {}) error {
238
241
return newLocationError (
239
- makeWrappedConstError (AlreadyExists , format , args ... ),
242
+ fmtErrWithType (AlreadyExists , false , format , args ... ),
240
243
1 ,
241
244
)
242
245
}
@@ -260,7 +263,7 @@ func IsAlreadyExists(err error) bool {
260
263
// Locationer interface.
261
264
func NotSupportedf (format string , args ... interface {}) error {
262
265
return newLocationError (
263
- makeWrappedConstError (NotSupported , format , args ... ),
266
+ fmtErrWithType (NotSupported , false , format , args ... ),
264
267
1 ,
265
268
)
266
269
}
@@ -284,7 +287,7 @@ func IsNotSupported(err error) bool {
284
287
// Locationer interface.
285
288
func NotValidf (format string , args ... interface {}) error {
286
289
return newLocationError (
287
- makeWrappedConstError (NotValid , format , args ... ),
290
+ fmtErrWithType (NotValid , false , format , args ... ),
288
291
1 ,
289
292
)
290
293
}
@@ -308,7 +311,7 @@ func IsNotValid(err error) bool {
308
311
// the Locationer interface.
309
312
func NotProvisionedf (format string , args ... interface {}) error {
310
313
return newLocationError (
311
- makeWrappedConstError (NotProvisioned , format , args ... ),
314
+ fmtErrWithType (NotProvisioned , false , format , args ... ),
312
315
1 ,
313
316
)
314
317
}
@@ -332,7 +335,7 @@ func IsNotProvisioned(err error) bool {
332
335
// Locationer interface.
333
336
func NotAssignedf (format string , args ... interface {}) error {
334
337
return newLocationError (
335
- makeWrappedConstError (NotAssigned , format , args ... ),
338
+ fmtErrWithType (NotAssigned , false , format , args ... ),
336
339
1 ,
337
340
)
338
341
}
@@ -356,7 +359,7 @@ func IsNotAssigned(err error) bool {
356
359
// Locationer interface.
357
360
func BadRequestf (format string , args ... interface {}) error {
358
361
return newLocationError (
359
- makeWrappedConstError ( Hide ( BadRequest ) , format , args ... ),
362
+ fmtErrWithType ( BadRequest , true , format , args ... ),
360
363
1 ,
361
364
)
362
365
}
@@ -380,7 +383,7 @@ func IsBadRequest(err error) bool {
380
383
// and the Locationer interface.
381
384
func MethodNotAllowedf (format string , args ... interface {}) error {
382
385
return newLocationError (
383
- makeWrappedConstError ( Hide ( MethodNotAllowed ) , format , args ... ),
386
+ fmtErrWithType ( MethodNotAllowed , true , format , args ... ),
384
387
1 ,
385
388
)
386
389
}
@@ -404,7 +407,7 @@ func IsMethodNotAllowed(err error) bool {
404
407
// Locationer interface.
405
408
func Forbiddenf (format string , args ... interface {}) error {
406
409
return newLocationError (
407
- makeWrappedConstError ( Hide ( Forbidden ) , format , args ... ),
410
+ fmtErrWithType ( Forbidden , true , format , args ... ),
408
411
1 ,
409
412
)
410
413
}
@@ -428,7 +431,7 @@ func IsForbidden(err error) bool {
428
431
// Is(err, QuotaLimitExceeded) and the Locationer interface.
429
432
func QuotaLimitExceededf (format string , args ... interface {}) error {
430
433
return newLocationError (
431
- makeWrappedConstError ( Hide ( QuotaLimitExceeded ) , format , args ... ),
434
+ fmtErrWithType ( QuotaLimitExceeded , true , format , args ... ),
432
435
1 ,
433
436
)
434
437
}
@@ -452,7 +455,7 @@ func IsQuotaLimitExceeded(err error) bool {
452
455
// and the Locationer interface.
453
456
func NotYetAvailablef (format string , args ... interface {}) error {
454
457
return newLocationError (
455
- makeWrappedConstError ( Hide ( NotYetAvailable ) , format , args ... ),
458
+ fmtErrWithType ( NotYetAvailable , true , format , args ... ),
456
459
1 ,
457
460
)
458
461
}
0 commit comments