mirror of https://github.com/gofiber/fiber.git
✨ v3 (feature): use any as default Message type of Error struct (#1925)
* ✨ v3: use any as default Message type of Error struct. * ✨ v3: use any as default Message type of Error struct. * ✨ v3: use any as default Message type of Error struct. * ✨ v3: use any as default Message type of Error struct.pull/1928/head
parent
9490b72700
commit
e35a594cf1
22
app.go
22
app.go
|
@ -86,7 +86,7 @@ type ErrorHandler = func(*Ctx, error) error
|
|||
// Error represents an error that occurred while handling a request.
|
||||
type Error struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Message any `json:"message"`
|
||||
}
|
||||
|
||||
// App denotes the Fiber application.
|
||||
|
@ -727,19 +727,25 @@ func (app *App) Route(prefix string, fn func(router Router), name ...string) Rou
|
|||
|
||||
// Error makes it compatible with the `error` interface.
|
||||
func (e *Error) Error() string {
|
||||
return e.Message
|
||||
return fmt.Sprint(e.Message)
|
||||
}
|
||||
|
||||
// NewError creates a new Error instance with an optional message
|
||||
func NewError(code int, message ...string) *Error {
|
||||
err := &Error{
|
||||
// NewErrors creates multiple/single new Error instances.
|
||||
// If you want to pass single message, you have to pass 1 message.
|
||||
// To pass multiple error messages, you have to pass +2 messages.
|
||||
func NewErrors(code int, messages ...any) *Error {
|
||||
e := &Error{
|
||||
Code: code,
|
||||
Message: utils.StatusMessage(code),
|
||||
}
|
||||
if len(message) > 0 {
|
||||
err.Message = message[0]
|
||||
|
||||
if len(messages) == 1 {
|
||||
e.Message = messages[0]
|
||||
} else if len(messages) > 1 {
|
||||
e.Message = messages
|
||||
}
|
||||
return err
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
// Listener can be used to pass a custom listener.
|
||||
|
|
16
app_test.go
16
app_test.go
|
@ -1255,11 +1255,17 @@ func Benchmark_AcquireCtx(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
// go test -run Test_NewError
|
||||
func Test_NewError(t *testing.T) {
|
||||
err := NewError(StatusForbidden, "permission denied")
|
||||
utils.AssertEqual(t, StatusForbidden, err.Code)
|
||||
utils.AssertEqual(t, "permission denied", err.Message)
|
||||
// go test -run Test_NewErrors
|
||||
func Test_NewErrors(t *testing.T) {
|
||||
e := NewErrors(StatusForbidden, "permission denied")
|
||||
utils.AssertEqual(t, StatusForbidden, e.Code)
|
||||
utils.AssertEqual(t, "permission denied", fmt.Sprint(e.Message))
|
||||
|
||||
e = NewErrors(StatusBadRequest, "error 1", "error 2")
|
||||
messages := e.Message.([]interface{})
|
||||
utils.AssertEqual(t, StatusBadRequest, e.Code)
|
||||
utils.AssertEqual(t, "error 1", fmt.Sprint(messages[0]))
|
||||
utils.AssertEqual(t, "error 2", fmt.Sprint(messages[1]))
|
||||
}
|
||||
|
||||
// go test -run Test_Test_Timeout
|
||||
|
|
2
ctx.go
2
ctx.go
|
@ -1407,7 +1407,7 @@ func (c *Ctx) SendFile(file string, compress ...bool) error {
|
|||
}
|
||||
// Check for error
|
||||
if status != StatusNotFound && fsStatus == StatusNotFound {
|
||||
return NewError(StatusNotFound, fmt.Sprintf("sendfile: file %s not found", filename))
|
||||
return NewErrors(StatusNotFound, fmt.Sprintf("sendfile: file %s not found", filename))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
80
helpers.go
80
helpers.go
|
@ -454,46 +454,46 @@ const (
|
|||
|
||||
// Errors
|
||||
var (
|
||||
ErrBadRequest = NewError(StatusBadRequest) // RFC 7231, 6.5.1
|
||||
ErrUnauthorized = NewError(StatusUnauthorized) // RFC 7235, 3.1
|
||||
ErrPaymentRequired = NewError(StatusPaymentRequired) // RFC 7231, 6.5.2
|
||||
ErrForbidden = NewError(StatusForbidden) // RFC 7231, 6.5.3
|
||||
ErrNotFound = NewError(StatusNotFound) // RFC 7231, 6.5.4
|
||||
ErrMethodNotAllowed = NewError(StatusMethodNotAllowed) // RFC 7231, 6.5.5
|
||||
ErrNotAcceptable = NewError(StatusNotAcceptable) // RFC 7231, 6.5.6
|
||||
ErrProxyAuthRequired = NewError(StatusProxyAuthRequired) // RFC 7235, 3.2
|
||||
ErrRequestTimeout = NewError(StatusRequestTimeout) // RFC 7231, 6.5.7
|
||||
ErrConflict = NewError(StatusConflict) // RFC 7231, 6.5.8
|
||||
ErrGone = NewError(StatusGone) // RFC 7231, 6.5.9
|
||||
ErrLengthRequired = NewError(StatusLengthRequired) // RFC 7231, 6.5.10
|
||||
ErrPreconditionFailed = NewError(StatusPreconditionFailed) // RFC 7232, 4.2
|
||||
ErrRequestEntityTooLarge = NewError(StatusRequestEntityTooLarge) // RFC 7231, 6.5.11
|
||||
ErrRequestURITooLong = NewError(StatusRequestURITooLong) // RFC 7231, 6.5.12
|
||||
ErrUnsupportedMediaType = NewError(StatusUnsupportedMediaType) // RFC 7231, 6.5.13
|
||||
ErrRequestedRangeNotSatisfiable = NewError(StatusRequestedRangeNotSatisfiable) // RFC 7233, 4.4
|
||||
ErrExpectationFailed = NewError(StatusExpectationFailed) // RFC 7231, 6.5.14
|
||||
ErrTeapot = NewError(StatusTeapot) // RFC 7168, 2.3.3
|
||||
ErrMisdirectedRequest = NewError(StatusMisdirectedRequest) // RFC 7540, 9.1.2
|
||||
ErrUnprocessableEntity = NewError(StatusUnprocessableEntity) // RFC 4918, 11.2
|
||||
ErrLocked = NewError(StatusLocked) // RFC 4918, 11.3
|
||||
ErrFailedDependency = NewError(StatusFailedDependency) // RFC 4918, 11.4
|
||||
ErrTooEarly = NewError(StatusTooEarly) // RFC 8470, 5.2.
|
||||
ErrUpgradeRequired = NewError(StatusUpgradeRequired) // RFC 7231, 6.5.15
|
||||
ErrPreconditionRequired = NewError(StatusPreconditionRequired) // RFC 6585, 3
|
||||
ErrTooManyRequests = NewError(StatusTooManyRequests) // RFC 6585, 4
|
||||
ErrRequestHeaderFieldsTooLarge = NewError(StatusRequestHeaderFieldsTooLarge) // RFC 6585, 5
|
||||
ErrUnavailableForLegalReasons = NewError(StatusUnavailableForLegalReasons) // RFC 7725, 3
|
||||
ErrInternalServerError = NewError(StatusInternalServerError) // RFC 7231, 6.6.1
|
||||
ErrNotImplemented = NewError(StatusNotImplemented) // RFC 7231, 6.6.2
|
||||
ErrBadGateway = NewError(StatusBadGateway) // RFC 7231, 6.6.3
|
||||
ErrServiceUnavailable = NewError(StatusServiceUnavailable) // RFC 7231, 6.6.4
|
||||
ErrGatewayTimeout = NewError(StatusGatewayTimeout) // RFC 7231, 6.6.5
|
||||
ErrHTTPVersionNotSupported = NewError(StatusHTTPVersionNotSupported) // RFC 7231, 6.6.6
|
||||
ErrVariantAlsoNegotiates = NewError(StatusVariantAlsoNegotiates) // RFC 2295, 8.1
|
||||
ErrInsufficientStorage = NewError(StatusInsufficientStorage) // RFC 4918, 11.5
|
||||
ErrLoopDetected = NewError(StatusLoopDetected) // RFC 5842, 7.2
|
||||
ErrNotExtended = NewError(StatusNotExtended) // RFC 2774, 7
|
||||
ErrNetworkAuthenticationRequired = NewError(StatusNetworkAuthenticationRequired) // RFC 6585, 6
|
||||
ErrBadRequest = NewErrors(StatusBadRequest) // RFC 7231, 6.5.1
|
||||
ErrUnauthorized = NewErrors(StatusUnauthorized) // RFC 7235, 3.1
|
||||
ErrPaymentRequired = NewErrors(StatusPaymentRequired) // RFC 7231, 6.5.2
|
||||
ErrForbidden = NewErrors(StatusForbidden) // RFC 7231, 6.5.3
|
||||
ErrNotFound = NewErrors(StatusNotFound) // RFC 7231, 6.5.4
|
||||
ErrMethodNotAllowed = NewErrors(StatusMethodNotAllowed) // RFC 7231, 6.5.5
|
||||
ErrNotAcceptable = NewErrors(StatusNotAcceptable) // RFC 7231, 6.5.6
|
||||
ErrProxyAuthRequired = NewErrors(StatusProxyAuthRequired) // RFC 7235, 3.2
|
||||
ErrRequestTimeout = NewErrors(StatusRequestTimeout) // RFC 7231, 6.5.7
|
||||
ErrConflict = NewErrors(StatusConflict) // RFC 7231, 6.5.8
|
||||
ErrGone = NewErrors(StatusGone) // RFC 7231, 6.5.9
|
||||
ErrLengthRequired = NewErrors(StatusLengthRequired) // RFC 7231, 6.5.10
|
||||
ErrPreconditionFailed = NewErrors(StatusPreconditionFailed) // RFC 7232, 4.2
|
||||
ErrRequestEntityTooLarge = NewErrors(StatusRequestEntityTooLarge) // RFC 7231, 6.5.11
|
||||
ErrRequestURITooLong = NewErrors(StatusRequestURITooLong) // RFC 7231, 6.5.12
|
||||
ErrUnsupportedMediaType = NewErrors(StatusUnsupportedMediaType) // RFC 7231, 6.5.13
|
||||
ErrRequestedRangeNotSatisfiable = NewErrors(StatusRequestedRangeNotSatisfiable) // RFC 7233, 4.4
|
||||
ErrExpectationFailed = NewErrors(StatusExpectationFailed) // RFC 7231, 6.5.14
|
||||
ErrTeapot = NewErrors(StatusTeapot) // RFC 7168, 2.3.3
|
||||
ErrMisdirectedRequest = NewErrors(StatusMisdirectedRequest) // RFC 7540, 9.1.2
|
||||
ErrUnprocessableEntity = NewErrors(StatusUnprocessableEntity) // RFC 4918, 11.2
|
||||
ErrLocked = NewErrors(StatusLocked) // RFC 4918, 11.3
|
||||
ErrFailedDependency = NewErrors(StatusFailedDependency) // RFC 4918, 11.4
|
||||
ErrTooEarly = NewErrors(StatusTooEarly) // RFC 8470, 5.2.
|
||||
ErrUpgradeRequired = NewErrors(StatusUpgradeRequired) // RFC 7231, 6.5.15
|
||||
ErrPreconditionRequired = NewErrors(StatusPreconditionRequired) // RFC 6585, 3
|
||||
ErrTooManyRequests = NewErrors(StatusTooManyRequests) // RFC 6585, 4
|
||||
ErrRequestHeaderFieldsTooLarge = NewErrors(StatusRequestHeaderFieldsTooLarge) // RFC 6585, 5
|
||||
ErrUnavailableForLegalReasons = NewErrors(StatusUnavailableForLegalReasons) // RFC 7725, 3
|
||||
ErrInternalServerError = NewErrors(StatusInternalServerError) // RFC 7231, 6.6.1
|
||||
ErrNotImplemented = NewErrors(StatusNotImplemented) // RFC 7231, 6.6.2
|
||||
ErrBadGateway = NewErrors(StatusBadGateway) // RFC 7231, 6.6.3
|
||||
ErrServiceUnavailable = NewErrors(StatusServiceUnavailable) // RFC 7231, 6.6.4
|
||||
ErrGatewayTimeout = NewErrors(StatusGatewayTimeout) // RFC 7231, 6.6.5
|
||||
ErrHTTPVersionNotSupported = NewErrors(StatusHTTPVersionNotSupported) // RFC 7231, 6.6.6
|
||||
ErrVariantAlsoNegotiates = NewErrors(StatusVariantAlsoNegotiates) // RFC 2295, 8.1
|
||||
ErrInsufficientStorage = NewErrors(StatusInsufficientStorage) // RFC 4918, 11.5
|
||||
ErrLoopDetected = NewErrors(StatusLoopDetected) // RFC 5842, 7.2
|
||||
ErrNotExtended = NewErrors(StatusNotExtended) // RFC 2774, 7
|
||||
ErrNetworkAuthenticationRequired = NewErrors(StatusNetworkAuthenticationRequired) // RFC 6585, 6
|
||||
)
|
||||
|
||||
// HTTP Headers were copied from net/http.
|
||||
|
|
|
@ -134,7 +134,7 @@ func (app *App) next(c *Ctx) (match bool, err error) {
|
|||
}
|
||||
|
||||
// If c.Next() does not match, return 404
|
||||
err = NewError(StatusNotFound, "Cannot "+c.method+" "+c.pathOriginal)
|
||||
err = NewErrors(StatusNotFound, "Cannot "+c.method+" "+c.pathOriginal)
|
||||
|
||||
// If no match, scan stack again if other methods match the request
|
||||
// Moved from app.handler because middleware may break the route chain
|
||||
|
|
Loading…
Reference in New Issue