Update error handler

pull/448/head
Fenny 2020-06-06 20:42:08 +02:00
parent 749aac17bb
commit f0a9846bf0
4 changed files with 23 additions and 19 deletions

28
app.go
View File

@ -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)
},
}

View File

@ -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!")
},
})

9
ctx.go
View File

@ -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 {

View File

@ -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
}
}()