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

🐛 Sub fiber's error handlers not working #1839

Closed
h4ckedneko opened this issue Mar 29, 2022 · 7 comments · Fixed by #1848
Closed

🐛 Sub fiber's error handlers not working #1839

h4ckedneko opened this issue Mar 29, 2022 · 7 comments · Fixed by #1848
Assignees

Comments

@h4ckedneko
Copy link

Fiber version
v2.30.0

Issue description
In v2.24.0, custom error handler in fiber sub apps is captured by the main fiber app when you mounted them (#1560). But this doesn't work anymore with the recent version.

Code snippet

package main

import "github.com/gofiber/fiber/v2"

// Custom error handler.
func apiErrorHandler = func(c *fiber.Ctx, err error) error {
    // Custom error handling logic here.
    // ...

    // Return from handler
    return nil
}

func main() {
  app := fiber.New()

  // Steps to reproduce  
  api := fiber.New(fiber.Config{
    ErrorHandler: apiErrorHandler,
  })
  
  app.Mount("/api", api)

  log.Fatal(app.Listen(":3000"))
}
@welcome
Copy link

welcome bot commented Mar 29, 2022

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@ReneWerner87
Copy link
Member

ReneWerner87 commented Mar 29, 2022

package main

import (
	"fmt"
	"log"

	"github.com/gofiber/fiber/v2"
)

// Custom error handler.
func apiErrorHandler(c *fiber.Ctx, err error) error {
	// Custom error handling logic here.
	// ...
	fmt.Println("apiErrorHandler", err)
	// Return from handler
	return fiber.DefaultErrorHandler(c, err)
}

func main() {
	app := fiber.New()

	// Steps to reproduce
	api := fiber.New(fiber.Config{
		ErrorHandler: apiErrorHandler,
	})

	api.Get("/test", func(ctx *fiber.Ctx) error {
		return fiber.ErrBadRequest
	})

	app.Mount("/api", api)

	log.Fatal(app.Listen(":3000"))
}
curl -i http://localhost:3000/api/test

can not reproduce, need more information on this

for me it works as seen in the screenshot
image

can you( @h4ckedneko ) refine your example or describe the error in more detail ?

@h4ckedneko
Copy link
Author

Hello, I created a git repo to reproduce this bug:
https://github.com/h4ckedneko/fiber-bug

@h4ckedneko
Copy link
Author

Both v2.25.0 and v2.31.0 have the same fiber setup. But if you run them, the recent version doesn't print "custom error handler called" meaning the custom error handler is not being registered.

@h4ckedneko
Copy link
Author

image
image

@ReneWerner87
Copy link
Member

okay found the error, it only occurs when you root mounted apps from other fiber instances

we will fix it, thanks for the info

related to the refactoring from :
https://github.com/gofiber/fiber/pull/1749/files

@h4ckedneko
Copy link
Author

Thanks Fiber team! 🥳

ReneWerner87 added a commit that referenced this issue Apr 5, 2022
trim21 pushed a commit to trim21/fiber that referenced this issue Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants