You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I create two sub apps with different error handlers and mount one of them on the β/β path and the other on the β/twoβ path.
If an error occurs in handler, the (app *App)ErrorHandler method can incorrectly define the error handler for the second sub app. You can see this in the code snippet.
This happens because the paths β/β and β/twoβ return the same number of parts (2) from strings.Split(prefix, β/β ) , and the path β/twoβ is prefixed with β/β. For the method to work correctly, the path β/β must be defined as only 1 part:
func (app *App) ErrorHandler(ctx *Ctx, err error) error {
...
for prefix, subApp := range app.mountFields.appList {
if prefix != "" && strings.HasPrefix(ctx.path, prefix) {
if prefix == "/" {
parts = 1
} else {
parts := len(strings.Split(prefix, "/"))
}
...
How to Reproduce
Steps to reproduce the behavior:
Run the code snippet (may take a few times).
Check the output in the stdout.
Expected Behavior
The error handler for the sub app must be defined correctly.
In my example, only the twoErrorHandler must be used for the sub app two.
Fiber Version
v2.52.0
Code Snippet (optional)
package main
import (
"errors""fmt""log""net/http""github.com/gofiber/fiber/v2"
)
// Custom error handlers.funconeErrorHandler(c*fiber.Ctx, errerror) error {
// Custom error handling logic here.fmt.Println("ONE error handler")
// Return from handlerreturnfiber.DefaultErrorHandler(c, err)
}
functwoErrorHandler(c*fiber.Ctx, errerror) error {
// Custom error handling logic here.fmt.Println("TWO error handler")
// Return from handlerreturnfiber.DefaultErrorHandler(c, err)
}
funchandler(c*fiber.Ctx) error {
// Return error to call ErrorHandlerreturnerrors.New("some error")
}
funcrunFiberApps() {
app:=fiber.New()
// Create first sub-app with custom error handler.one:=fiber.New(fiber.Config{
ErrorHandler: oneErrorHandler,
})
one.Get("/some-path", handler)
app.Mount("/", one)
// Create second sub-app with custom error handler.two:=fiber.New(fiber.Config{
ErrorHandler: twoErrorHandler,
})
two.Get("/some-path", handler)
app.Mount("/two", two)
log.Fatal(app.Listen(":3000"))
}
funcmain() {
// Create one fiber.App with two sub apps and start listen on :3000.gorunFiberApps()
// Multiple calls to the handler "two"// to see a random error handler calls.fori:=0; i<20; i++ {
_, _=http.Get("https://localhost:3000/two/some-path")
}
fmt.Println("the end")
}
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
Bug Description
I create two sub apps with different error handlers and mount one of them on the β/β path and the other on the β/twoβ path.
If an error occurs in handler, the (app *App)ErrorHandler method can incorrectly define the error handler for the second sub app. You can see this in the code snippet.
This happens because the paths β/β and β/twoβ return the same number of parts (2) from strings.Split(prefix, β/β ) , and the path β/twoβ is prefixed with β/β. For the method to work correctly, the path β/β must be defined as only 1 part:
How to Reproduce
Steps to reproduce the behavior:
Expected Behavior
The error handler for the sub app must be defined correctly.
In my example, only the
twoErrorHandler
must be used for the sub apptwo
.Fiber Version
v2.52.0
Code Snippet (optional)
Checklist:
The text was updated successfully, but these errors were encountered: