mirror of https://github.com/gofiber/fiber.git
🩹 fix: backwards incompatible change to fiber.Error (#1768)
* fix: backwards incompatible change to fiber.Error * revert: work backward compatiblepull/1779/head
parent
391ae594d8
commit
74a20b4589
26
app.go
26
app.go
|
@ -84,8 +84,8 @@ type ErrorHandler = func(*Ctx, error) error
|
||||||
|
|
||||||
// Error represents an error that occurred while handling a request.
|
// Error represents an error that occurred while handling a request.
|
||||||
type Error struct {
|
type Error struct {
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
Message interface{} `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// App denotes the Fiber application.
|
// App denotes the Fiber application.
|
||||||
|
@ -722,31 +722,19 @@ func (app *App) Route(prefix string, fn func(router Router), name ...string) Rou
|
||||||
|
|
||||||
// Error makes it compatible with the `error` interface.
|
// Error makes it compatible with the `error` interface.
|
||||||
func (e *Error) Error() string {
|
func (e *Error) Error() string {
|
||||||
return fmt.Sprint(e.Message)
|
return e.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewError creates a new Error instance with an optional message
|
// NewError creates a new Error instance with an optional message
|
||||||
func NewError(code int, message ...interface{}) *Error {
|
func NewError(code int, message ...string) *Error {
|
||||||
e := &Error{
|
err := &Error{
|
||||||
Code: code,
|
Code: code,
|
||||||
Message: utils.StatusMessage(code),
|
Message: utils.StatusMessage(code),
|
||||||
}
|
}
|
||||||
if len(message) > 0 {
|
if len(message) > 0 {
|
||||||
e.Message = message[0]
|
err.Message = message[0]
|
||||||
}
|
}
|
||||||
return e
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
// NewErrors creates multiple new Error messages
|
|
||||||
func NewErrors(code int, messages ...interface{}) *Error {
|
|
||||||
e := &Error{
|
|
||||||
Code: code,
|
|
||||||
Message: utils.StatusMessage(code),
|
|
||||||
}
|
|
||||||
if len(messages) > 0 {
|
|
||||||
e.Message = messages
|
|
||||||
}
|
|
||||||
return e
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listener can be used to pass a custom listener.
|
// Listener can be used to pass a custom listener.
|
||||||
|
|
14
app_test.go
14
app_test.go
|
@ -1230,17 +1230,9 @@ func Benchmark_App_ETag_Weak(b *testing.B) {
|
||||||
|
|
||||||
// go test -run Test_NewError
|
// go test -run Test_NewError
|
||||||
func Test_NewError(t *testing.T) {
|
func Test_NewError(t *testing.T) {
|
||||||
e := NewError(StatusForbidden, "permission denied")
|
err := NewError(StatusForbidden, "permission denied")
|
||||||
utils.AssertEqual(t, StatusForbidden, e.Code)
|
utils.AssertEqual(t, StatusForbidden, err.Code)
|
||||||
utils.AssertEqual(t, "permission denied", fmt.Sprint(e.Message))
|
utils.AssertEqual(t, "permission denied", err.Message)
|
||||||
}
|
|
||||||
|
|
||||||
func Test_NewErrors(t *testing.T) {
|
|
||||||
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
|
// go test -run Test_Test_Timeout
|
||||||
|
|
Loading…
Reference in New Issue