@@ -25,7 +25,7 @@ type Option func(e *Error) error
25
25
// message only if it is empty.
26
26
func withDefaultMessage (format string , args ... interface {}) Option {
27
27
return func (e * Error ) error {
28
- if len ( e .Msg ) > 0 {
28
+ if e .Msg != "" {
29
29
return e
30
30
}
31
31
e .Msg = fmt .Sprintf (format , args ... )
@@ -164,7 +164,8 @@ type Messenger interface {
164
164
func StatusCodeError (code int , e error , opts ... Option ) error {
165
165
switch code {
166
166
case http .StatusBadRequest :
167
- return BadRequestErr (e , opts ... )
167
+ opts = append (opts , withDefaultMessage (BadRequestDefaultMsg ))
168
+ return NewErr (http .StatusBadRequest , e , opts ... )
168
169
case http .StatusUnauthorized :
169
170
return UnauthorizedErr (e , opts ... )
170
171
case http .StatusForbidden :
@@ -200,6 +201,15 @@ var (
200
201
BadRequestPrefix = "The request could not be completed: "
201
202
)
202
203
204
+ func formatMessage (status int , msg string ) string {
205
+ switch status {
206
+ case http .StatusBadRequest :
207
+ return BadRequestPrefix + msg + "."
208
+ default :
209
+ return msg
210
+ }
211
+ }
212
+
203
213
// splitOptionArgs splits the variadic length args into string formatting args
204
214
// and Option(s) to apply to an Error.
205
215
func splitOptionArgs (args []interface {}) ([]interface {}, []Option ) {
@@ -229,11 +239,24 @@ func New(status int, format string, args ...interface{}) error {
229
239
msg := fmt .Sprintf (format , args ... )
230
240
return & Error {
231
241
Status : status ,
232
- Msg : msg ,
242
+ Msg : formatMessage ( status , msg ) ,
233
243
Err : errors .New (msg ),
234
244
}
235
245
}
236
246
247
+ // NewError creates a new http error with the given error and message.
248
+ func NewError (status int , err error , format string , args ... interface {}) error {
249
+ msg := fmt .Sprintf (format , args ... )
250
+ if _ , ok := err .(StackTracer ); ! ok {
251
+ err = errors .Wrap (err , msg )
252
+ }
253
+ return & Error {
254
+ Status : status ,
255
+ Msg : formatMessage (status , msg ),
256
+ Err : err ,
257
+ }
258
+ }
259
+
237
260
// NewErr returns a new Error. If the given error implements the StatusCoder
238
261
// interface we will ignore the given status.
239
262
func NewErr (status int , err error , opts ... Option ) error {
@@ -308,14 +331,12 @@ func NotImplementedErr(err error, opts ...Option) error {
308
331
309
332
// BadRequest creates a 400 error with the given format and arguments.
310
333
func BadRequest (format string , args ... interface {}) error {
311
- format = BadRequestPrefix + format + "."
312
334
return New (http .StatusBadRequest , format , args ... )
313
335
}
314
336
315
337
// BadRequestErr returns an 400 error with the given error.
316
- func BadRequestErr (err error , opts ... Option ) error {
317
- opts = append (opts , withDefaultMessage (BadRequestDefaultMsg ))
318
- return NewErr (http .StatusBadRequest , err , opts ... )
338
+ func BadRequestErr (err error , format string , args ... interface {}) error {
339
+ return NewError (http .StatusBadRequest , err , format , args ... )
319
340
}
320
341
321
342
// Unauthorized creates a 401 error with the given format and arguments.
0 commit comments