Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke twice CustomErrorHandler #26

Closed
itivanov opened this issue Aug 17, 2024 · 3 comments
Closed

Invoke twice CustomErrorHandler #26

itivanov opened this issue Aug 17, 2024 · 3 comments

Comments

@itivanov
Copy link

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
Copy link

KScaesar commented Sep 13, 2024

I have the same issue.

slog-fiber Retrieve errHandler = c.App().ErrorHandler

slog-fiber/middleware.go

Lines 110 to 119 in bf0203e

func NewWithConfig(logger *slog.Logger, config Config) fiber.Handler {
var (
once sync.Once
errHandler fiber.ErrorHandler
)
return func(c *fiber.Ctx) error {
once.Do(func() {
errHandler = c.App().ErrorHandler
})

after error handler execue, it should override the original err

slog-fiber/middleware.go

Lines 134 to 139 in bf0203e

err := c.Next()
if err != nil {
if err := errHandler(c, err); err != nil {
_ = c.SendStatus(fiber.StatusInternalServerError) //nolint:errcheck
}
}

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

@samber
Copy link
Owner

samber commented Sep 17, 2024

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

@KScaesar
Copy link

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

@samber samber closed this as completed Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants