samber / slog-fiber

🚨 Fiber middleware for slog logger
https://pkg.go.dev/github.com/samber/slog-fiber
MIT License
57 stars 9 forks source link

Invoke twice CustomErrorHandler #26

Closed itivanov closed 1 month ago

itivanov commented 2 months ago

With this setup error handler is called twice

app := fiber.New(fiber.Config{
    ErrorHandler: CustomErrorHandler,
})

...

func CustomErrorHandler(c *fiber.Ctx, err error) error {
    fmt.Println(err)

    code := fiber.StatusInternalServerError

    var e *fiber.Error
    if errors.As(err, &e) {
        code = e.Code
    }

    c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)

    return c.Status(code).SendString(err.Error())
}
KScaesar commented 1 month ago

I have the same issue.

slog-fiber Retrieve errHandler = c.App().ErrorHandler https://github.com/samber/slog-fiber/blob/bf0203e01f2a1a306a56495794cee3b28c2ef2c8/middleware.go#L110-L119

after error handler execue, it should override the original err https://github.com/samber/slog-fiber/blob/bf0203e01f2a1a306a56495794cee3b28c2ef2c8/middleware.go#L134-L139

FIX: (X) if err := errHandler(c, err); err != nil { (O) if err = errHandler(c, err); err != nil {

samber commented 1 month ago

@KScaesar i just made a fix in 1.16.3. Can you test it please ?

KScaesar commented 1 month ago

@KScaesar i just made a fix in 1.16.3. Can you test it please ?

nice fix!

The error handling in version 1.16.3 will only occur once.