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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Custom error handler not working for a mounted app instance #1438

Closed
zeghong opened this issue Jul 15, 2021 · 5 comments 路 Fixed by #1560
Closed

馃悰 Custom error handler not working for a mounted app instance #1438

zeghong opened this issue Jul 15, 2021 · 5 comments 路 Fixed by #1560

Comments

@zeghong
Copy link

zeghong commented Jul 15, 2021

Fiber version

v2.14.0

Issue description

I'm creating a sub App instance with a custom error handler, then mount it to the main App instance. I found the custom error handler for the sub App instance is not effective.

I know this may not be a programming bug, but it seems like an unexpected behavior to me.

Also, i believe resolving this issue will help to address the following use case:
Imaging I have an HTTP service (main App instance) with two sub App instances, one for public API access and one for private API access. I'd like to custom different error handling for each instance, for example, outputting error logs to different targets.

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 Jul 15, 2021

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

@AliasT
Copy link

AliasT commented Jul 15, 2021

// may be error middleware on api ?
api.Use(someErrorMiddleware)

@zeghong
Copy link
Author

zeghong commented Jul 15, 2021

@AliasT Thanks for your advice, but i think middleware is incapable of catching and overriding error from the framework, for example, overriding the default plaintext 404 path not found error response to a json response.

@ReneWerner87
Copy link
Member

via mount we only copy the routes and handlers, but not the settings of the app.

@josebalius
Copy link
Contributor

I've opened a PR #1560 to solve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants