diff --git a/app.go b/app.go index 3b45b307b2..55896ae7d5 100644 --- a/app.go +++ b/app.go @@ -110,10 +110,10 @@ type App struct { latestRoute *Route // TLS handler tlsHandler *TLSHandler - // custom method check - customMethod bool // Mount fields mountFields *mountFields + // Indicates if the value was explicitly configured + configured Config } // Config is a struct holding the server settings. @@ -513,6 +513,9 @@ func New(config ...Config) *App { app.config = config[0] } + // Initialize configured before defaults are set + app.configured = app.config + if app.config.ETag { if !IsChild() { fmt.Println("[Warning] Config.ETag is deprecated since v2.0.6, please use 'middleware/etag'.") @@ -557,8 +560,6 @@ func New(config ...Config) *App { } if len(app.config.RequestMethods) == 0 { app.config.RequestMethods = DefaultMethods - } else { - app.customMethod = true } app.config.trustedProxiesMap = make(map[string]struct{}, len(app.config.TrustedProxies)) @@ -999,7 +1000,10 @@ func (app *App) ErrorHandler(ctx *Ctx, err error) error { if prefix != "" && strings.HasPrefix(ctx.path, prefix) { parts := len(strings.Split(prefix, "/")) if mountedPrefixParts <= parts { - mountedErrHandler = subApp.config.ErrorHandler + if subApp.configured.ErrorHandler != nil { + mountedErrHandler = subApp.config.ErrorHandler + } + mountedPrefixParts = parts } } diff --git a/helpers.go b/helpers.go index 11364e7334..34b9350b64 100644 --- a/helpers.go +++ b/helpers.go @@ -334,7 +334,7 @@ var getBytesImmutable = func(s string) (b []byte) { // HTTP methods and their unique INTs func (app *App) methodInt(s string) int { // For better performance - if !app.customMethod { + if len(app.configured.RequestMethods) == 0 { switch s { case MethodGet: return 0 diff --git a/mount_test.go b/mount_test.go index deee3ad4c5..1e944d09ba 100644 --- a/mount_test.go +++ b/mount_test.go @@ -139,6 +139,24 @@ func Test_App_Group_Mount(t *testing.T) { utils.AssertEqual(t, uint32(2), app.handlersCount) } +func Test_App_UseParentErrorHandler(t *testing.T) { + app := New(Config{ + ErrorHandler: func(ctx *Ctx, err error) error { + return ctx.Status(500).SendString("hi, i'm a custom error") + }, + }) + + fiber := New() + fiber.Get("/", func(c *Ctx) error { + return errors.New("something happened") + }) + + app.Mount("/api", fiber) + + resp, err := app.Test(httptest.NewRequest(MethodGet, "/api", nil)) + testErrorResponse(t, err, resp, "hi, i'm a custom error") +} + func Test_App_UseMountedErrorHandler(t *testing.T) { app := New()