diff --git a/app.go b/app.go index 0d840566..324821b5 100644 --- a/app.go +++ b/app.go @@ -52,13 +52,16 @@ type App struct { // Settings holds is a struct holding the server settings type Settings struct { - // Possible feature for v1.11.x // ErrorHandler is executed when you pass an error in the Next(err) method // This function is also executed when middleware.Recover() catches a panic - // Default: func(ctx *fiber.Ctx, err error) { - // ctx.Status(fiber.StatusBadRequest).SendString(err.Error()) + // Default: func(ctx *Ctx, err error) { + // code := StatusInternalServerError + // if e, ok := err.(*Error); ok { + // code = e.Code + // } + // ctx.Status(code).SendString(err.Error()) // } - ErrorHandler func(*Ctx, error) + ErrorHandler Handler // Enables the "Server: value" HTTP header. // Default: "" @@ -217,15 +220,16 @@ func New(settings ...*Settings) *App { Prefork: utils.GetArgument("-prefork"), BodyLimit: 4 * 1024 * 1024, Concurrency: 256 * 1024, - ErrorHandler: func(ctx *Ctx, err error) { + ErrorHandler: func(ctx *Ctx) { code := StatusInternalServerError - if e, ok := err.(*Error); ok { + if e, ok := ctx.Error().(*Error); ok { code = e.Code } - ctx.Status(code).SendString(err.Error()) + ctx.Status(code).SendString(ctx.Error().Error()) }, }, } + // Overwrite settings if provided if len(settings) > 0 { app.Settings = settings[0] @@ -243,14 +247,14 @@ func New(settings ...*Settings) *App { getBytes = getBytesImmutable getString = getStringImmutable } - // Possible feature for v1.11.x + // Set default error handler if app.Settings.ErrorHandler == nil { - app.Settings.ErrorHandler = func(ctx *Ctx, err error) { + app.Settings.ErrorHandler = func(ctx *Ctx) { code := StatusInternalServerError - if e, ok := err.(*Error); ok { + if e, ok := ctx.Error().(*Error); ok { code = e.Code } - ctx.Status(code).SendString(err.Error()) + ctx.Status(code).SendString(ctx.Error().Error()) } } } @@ -554,7 +558,7 @@ func (app *App) init() *App { } else { ctx.err = ErrBadRequest } - app.Settings.ErrorHandler(ctx, ctx.err) // ctx.Route() not available + app.Settings.ErrorHandler(ctx) // ctx.Route() not available app.ReleaseCtx(ctx) }, } diff --git a/app_test.go b/app_test.go index b9f542bc..bf015028 100644 --- a/app_test.go +++ b/app_test.go @@ -53,7 +53,7 @@ func Test_App_ErrorHandler(t *testing.T) { func Test_App_ErrorHandler_Custom(t *testing.T) { app := New(&Settings{ - ErrorHandler: func(ctx *Ctx, err error) { + ErrorHandler: func(ctx *Ctx) { ctx.Status(200).SendString("Hi, I'm an custom error!") }, }) diff --git a/ctx.go b/ctx.go index 29e08ec0..6ca3949d 100644 --- a/ctx.go +++ b/ctx.go @@ -40,7 +40,7 @@ type Ctx struct { path string // Prettified HTTP path pathOriginal string // Original HTTP path values []string // Route parameter values - err error // Contains error if caught + err error // Contains error if passed to Next Fasthttp *fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx } @@ -591,9 +591,9 @@ func (ctx *Ctx) Next(err ...error) { return } if len(err) > 0 { - ctx.err = err[0] ctx.Fasthttp.Response.Header.Reset() - ctx.app.Settings.ErrorHandler(ctx, err[0]) + ctx.app.Settings.ErrorHandler(ctx) + ctx.err = err[0] return } @@ -833,7 +833,8 @@ func (ctx *Ctx) SendFile(file string, compress ...bool) { hasTrailingSlash := len(file) > 0 && file[len(file)-1] == '/' var err error if file, err = filepath.Abs(file); err != nil { - ctx.app.Settings.ErrorHandler(ctx, err) + ctx.err = err + ctx.app.Settings.ErrorHandler(ctx) return } if hasTrailingSlash { diff --git a/middleware/recover.go b/middleware/recover.go index ba686bef..2ea24b33 100644 --- a/middleware/recover.go +++ b/middleware/recover.go @@ -15,8 +15,7 @@ func Recover() fiber.Handler { if !ok { err = fmt.Errorf("%v", r) } - ctx.Fasthttp.Response.Header.Reset() - ctx.App().Settings.ErrorHandler(ctx, err) + ctx.Next(err) return } }()