Skip to content

Commit c24cd07

Browse files
authored
Added wrappers for the remaining error codes defined in the standard library. (#876)
1 parent b4a4ec8 commit c24cd07

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

error.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ func Error401Unauthorized(msg string, errs ...error) StatusError {
287287
return NewError(http.StatusUnauthorized, msg, errs...)
288288
}
289289

290+
// Error402PaymentRequired returns a 402.
291+
func Error402PaymentRequired(msg string, errs ...error) StatusError {
292+
return NewError(http.StatusPaymentRequired, msg, errs...)
293+
}
294+
290295
// Error403Forbidden returns a 403.
291296
func Error403Forbidden(msg string, errs ...error) StatusError {
292297
return NewError(http.StatusForbidden, msg, errs...)
@@ -307,6 +312,16 @@ func Error406NotAcceptable(msg string, errs ...error) StatusError {
307312
return NewError(http.StatusNotAcceptable, msg, errs...)
308313
}
309314

315+
// Error407ProxyAuthRequired returns a 407.
316+
func Error407ProxyAuthRequired(msg string, errs ...error) StatusError {
317+
return NewError(http.StatusProxyAuthRequired, msg, errs...)
318+
}
319+
320+
// Error408RequestTimeout returns a 408.
321+
func Error408RequestTimeout(msg string, errs ...error) StatusError {
322+
return NewError(http.StatusRequestTimeout, msg, errs...)
323+
}
324+
310325
// Error409Conflict returns a 409.
311326
func Error409Conflict(msg string, errs ...error) StatusError {
312327
return NewError(http.StatusConflict, msg, errs...)
@@ -317,26 +332,96 @@ func Error410Gone(msg string, errs ...error) StatusError {
317332
return NewError(http.StatusGone, msg, errs...)
318333
}
319334

335+
// Error411LengthRequired returns a 411.
336+
func Error411LengthRequired(msg string, errs ...error) StatusError {
337+
return NewError(http.StatusLengthRequired, msg, errs...)
338+
}
339+
320340
// Error412PreconditionFailed returns a 412.
321341
func Error412PreconditionFailed(msg string, errs ...error) StatusError {
322342
return NewError(http.StatusPreconditionFailed, msg, errs...)
323343
}
324344

345+
// Error413RequestEntityTooLarge returns a 413.
346+
func Error413RequestEntityTooLarge(msg string, errs ...error) StatusError {
347+
return NewError(http.StatusRequestEntityTooLarge, msg, errs...)
348+
}
349+
350+
// Error414RequestURITooLong returns a 414.
351+
func Error414RequestURITooLong(msg string, errs ...error) StatusError {
352+
return NewError(http.StatusRequestURITooLong, msg, errs...)
353+
}
354+
325355
// Error415UnsupportedMediaType returns a 415.
326356
func Error415UnsupportedMediaType(msg string, errs ...error) StatusError {
327357
return NewError(http.StatusUnsupportedMediaType, msg, errs...)
328358
}
329359

360+
// Error416RequestedRangeNotSatisfiable returns a 416.
361+
func Error416RequestedRangeNotSatisfiable(msg string, errs ...error) StatusError {
362+
return NewError(http.StatusRequestedRangeNotSatisfiable, msg, errs...)
363+
}
364+
365+
// Error417ExpectationFailed returns a 417.
366+
func Error417ExpectationFailed(msg string, errs ...error) StatusError {
367+
return NewError(http.StatusExpectationFailed, msg, errs...)
368+
}
369+
370+
// Error418Teapot returns a 418.
371+
func Error418Teapot(msg string, errs ...error) StatusError {
372+
return NewError(http.StatusTeapot, msg, errs...)
373+
}
374+
375+
// Error421MisdirectedRequest returns a 421.
376+
func Error421MisdirectedRequest(msg string, errs ...error) StatusError {
377+
return NewError(http.StatusMisdirectedRequest, msg, errs...)
378+
}
379+
330380
// Error422UnprocessableEntity returns a 422.
331381
func Error422UnprocessableEntity(msg string, errs ...error) StatusError {
332382
return NewError(http.StatusUnprocessableEntity, msg, errs...)
333383
}
334384

385+
// Error423Locked returns a 423.
386+
func Error423Locked(msg string, errs ...error) StatusError {
387+
return NewError(http.StatusLocked, msg, errs...)
388+
}
389+
390+
// Error424FailedDependency returns a 424.
391+
func Error424FailedDependency(msg string, errs ...error) StatusError {
392+
return NewError(http.StatusFailedDependency, msg, errs...)
393+
}
394+
395+
// Error425TooEarly returns a 425.
396+
func Error425TooEarly(msg string, errs ...error) StatusError {
397+
return NewError(http.StatusTooEarly, msg, errs...)
398+
}
399+
400+
// Error426UpgradeRequired returns a 426.
401+
func Error426UpgradeRequired(msg string, errs ...error) StatusError {
402+
return NewError(http.StatusUpgradeRequired, msg, errs...)
403+
}
404+
405+
// Error428PreconditionRequired returns a 428.
406+
func Error428PreconditionRequired(msg string, errs ...error) StatusError {
407+
return NewError(http.StatusPreconditionRequired, msg, errs...)
408+
}
409+
335410
// Error429TooManyRequests returns a 429.
336411
func Error429TooManyRequests(msg string, errs ...error) StatusError {
337412
return NewError(http.StatusTooManyRequests, msg, errs...)
338413
}
339414

415+
// Error431RequestHeaderFieldsTooLarge returns a 431.
416+
func Error431RequestHeaderFieldsTooLarge(msg string, errs ...error) StatusError {
417+
return NewError(http.StatusRequestHeaderFieldsTooLarge, msg, errs...)
418+
}
419+
420+
// Error451UnavailableForLegalReasons returns a 451.
421+
func Error451UnavailableForLegalReasons(msg string, errs ...error) StatusError {
422+
return NewError(http.StatusUnavailableForLegalReasons, msg, errs...)
423+
}
424+
340425
// Error500InternalServerError returns a 500.
341426
func Error500InternalServerError(msg string, errs ...error) StatusError {
342427
return NewError(http.StatusInternalServerError, msg, errs...)
@@ -362,5 +447,35 @@ func Error504GatewayTimeout(msg string, errs ...error) StatusError {
362447
return NewError(http.StatusGatewayTimeout, msg, errs...)
363448
}
364449

450+
// Error505HTTPVersionNotSupported returns a 505.
451+
func Error505HTTPVersionNotSupported(msg string, errs ...error) StatusError {
452+
return NewError(http.StatusHTTPVersionNotSupported, msg, errs...)
453+
}
454+
455+
// Error506VariantAlsoNegotiates returns a 506.
456+
func Error506VariantAlsoNegotiates(msg string, errs ...error) StatusError {
457+
return NewError(http.StatusVariantAlsoNegotiates, msg, errs...)
458+
}
459+
460+
// Error507InsufficientStorage returns a 507.
461+
func Error507InsufficientStorage(msg string, errs ...error) StatusError {
462+
return NewError(http.StatusInsufficientStorage, msg, errs...)
463+
}
464+
465+
// Error508LoopDetected returns a 508.
466+
func Error508LoopDetected(msg string, errs ...error) StatusError {
467+
return NewError(http.StatusLoopDetected, msg, errs...)
468+
}
469+
470+
// Error510NotExtended returns a 510.
471+
func Error510NotExtended(msg string, errs ...error) StatusError {
472+
return NewError(http.StatusNotExtended, msg, errs...)
473+
}
474+
475+
// Error511NetworkAuthenticationRequired returns a 511.
476+
func Error511NetworkAuthenticationRequired(msg string, errs ...error) StatusError {
477+
return NewError(http.StatusNetworkAuthenticationRequired, msg, errs...)
478+
}
479+
365480
// ErrorFormatter is a function that formats an error message
366481
var ErrorFormatter = fmt.Sprintf

error_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,49 @@ func TestErrorResponses(t *testing.T) {
5555
constructor func(msg string, errs ...error) huma.StatusError
5656
expected int
5757
}{
58+
// Client errors.
5859
{huma.Error400BadRequest, 400},
5960
{huma.Error401Unauthorized, 401},
61+
{huma.Error402PaymentRequired, 402},
6062
{huma.Error403Forbidden, 403},
6163
{huma.Error404NotFound, 404},
6264
{huma.Error405MethodNotAllowed, 405},
6365
{huma.Error406NotAcceptable, 406},
66+
{huma.Error407ProxyAuthRequired, 407},
67+
{huma.Error408RequestTimeout, 408},
6468
{huma.Error409Conflict, 409},
6569
{huma.Error410Gone, 410},
70+
{huma.Error411LengthRequired, 411},
6671
{huma.Error412PreconditionFailed, 412},
72+
{huma.Error413RequestEntityTooLarge, 413},
73+
{huma.Error414RequestURITooLong, 414},
6774
{huma.Error415UnsupportedMediaType, 415},
75+
{huma.Error416RequestedRangeNotSatisfiable, 416},
76+
{huma.Error417ExpectationFailed, 417},
77+
{huma.Error418Teapot, 418},
78+
{huma.Error421MisdirectedRequest, 421},
6879
{huma.Error422UnprocessableEntity, 422},
80+
{huma.Error423Locked, 423},
81+
{huma.Error424FailedDependency, 424},
82+
{huma.Error425TooEarly, 425},
83+
{huma.Error426UpgradeRequired, 426},
84+
{huma.Error428PreconditionRequired, 428},
6985
{huma.Error429TooManyRequests, 429},
86+
{huma.Error431RequestHeaderFieldsTooLarge, 431},
87+
{huma.Error451UnavailableForLegalReasons, 451},
88+
89+
// Server errors.
7090
{huma.Error500InternalServerError, 500},
7191
{huma.Error501NotImplemented, 501},
7292
{huma.Error502BadGateway, 502},
7393
{huma.Error503ServiceUnavailable, 503},
7494
{huma.Error504GatewayTimeout, 504},
95+
{huma.Error505HTTPVersionNotSupported, 505},
96+
{huma.Error506VariantAlsoNegotiates, 506},
97+
{huma.Error507InsufficientStorage, 507},
98+
{huma.Error508LoopDetected, 508},
99+
{huma.Error510NotExtended, 510},
100+
{huma.Error511NetworkAuthenticationRequired, 511},
75101
} {
76102
err := item.constructor("test")
77103
assert.Equal(t, item.expected, err.GetStatus())

0 commit comments

Comments
 (0)