From 2ed072b8bee2de3e63f0f2c59bde7539fa37fec5 Mon Sep 17 00:00:00 2001 From: Caleb Case Date: Mon, 14 Nov 2022 13:35:18 -0500 Subject: [PATCH 1/5] WIP: Use Parent Error Handler on Mount --- mount_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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() From b47a5552dc6325f5bad621a55eede95065c6377e Mon Sep 17 00:00:00 2001 From: Caleb Case Date: Mon, 14 Nov 2022 13:56:37 -0500 Subject: [PATCH 2/5] Add suggested boolean guard --- app.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index 3b45b307b2..b9e714ab0d 100644 --- a/app.go +++ b/app.go @@ -248,6 +248,9 @@ type Config struct { // Default: DefaultErrorHandler ErrorHandler ErrorHandler `json:"-"` + // ErrorHandlerConfigured is true when the fiber.ErrorHandler is configured explicitly. + ErrorHandlerConfigured bool `json:"-"` + // When set to true, disables keep-alive connections. // The server will close incoming connections after sending the first response to client. // @@ -541,6 +544,8 @@ func New(config ...Config) *App { if app.config.ErrorHandler == nil { app.config.ErrorHandler = DefaultErrorHandler + } else { + app.config.ErrorHandlerConfigured = true } if app.config.JSONEncoder == nil { @@ -999,7 +1004,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.config.ErrorHandlerConfigured { + mountedErrHandler = subApp.config.ErrorHandler + } + mountedPrefixParts = parts } } From 5a9cdfb2979d8e0a737bc50188486c99b460c8df Mon Sep 17 00:00:00 2001 From: Caleb Case Date: Mon, 14 Nov 2022 14:01:44 -0500 Subject: [PATCH 3/5] Move flag to App --- app.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app.go b/app.go index b9e714ab0d..cb2e57cb2f 100644 --- a/app.go +++ b/app.go @@ -114,6 +114,8 @@ type App struct { customMethod bool // Mount fields mountFields *mountFields + // True if the ErrorHandler is explicitly configured + errorHandlerConfigured bool } // Config is a struct holding the server settings. @@ -248,9 +250,6 @@ type Config struct { // Default: DefaultErrorHandler ErrorHandler ErrorHandler `json:"-"` - // ErrorHandlerConfigured is true when the fiber.ErrorHandler is configured explicitly. - ErrorHandlerConfigured bool `json:"-"` - // When set to true, disables keep-alive connections. // The server will close incoming connections after sending the first response to client. // @@ -545,7 +544,7 @@ func New(config ...Config) *App { if app.config.ErrorHandler == nil { app.config.ErrorHandler = DefaultErrorHandler } else { - app.config.ErrorHandlerConfigured = true + app.errorHandlerConfigured = true } if app.config.JSONEncoder == nil { @@ -1004,7 +1003,7 @@ func (app *App) ErrorHandler(ctx *Ctx, err error) error { if prefix != "" && strings.HasPrefix(ctx.path, prefix) { parts := len(strings.Split(prefix, "/")) if mountedPrefixParts <= parts { - if subApp.config.ErrorHandlerConfigured { + if subApp.errorHandlerConfigured { mountedErrHandler = subApp.config.ErrorHandler } From f74af73dc0aa553f6b37fbc671369675f4f5be8d Mon Sep 17 00:00:00 2001 From: Caleb Case Date: Mon, 14 Nov 2022 14:19:20 -0500 Subject: [PATCH 4/5] Move to copy of config as configured --- app.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app.go b/app.go index cb2e57cb2f..b0b78bd618 100644 --- a/app.go +++ b/app.go @@ -114,8 +114,8 @@ type App struct { customMethod bool // Mount fields mountFields *mountFields - // True if the ErrorHandler is explicitly configured - errorHandlerConfigured bool + // Indicates if the value was explicitly configured + configured Config } // Config is a struct holding the server settings. @@ -515,6 +515,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'.") @@ -543,8 +546,6 @@ func New(config ...Config) *App { if app.config.ErrorHandler == nil { app.config.ErrorHandler = DefaultErrorHandler - } else { - app.errorHandlerConfigured = true } if app.config.JSONEncoder == nil { @@ -1003,7 +1004,7 @@ func (app *App) ErrorHandler(ctx *Ctx, err error) error { if prefix != "" && strings.HasPrefix(ctx.path, prefix) { parts := len(strings.Split(prefix, "/")) if mountedPrefixParts <= parts { - if subApp.errorHandlerConfigured { + if subApp.configured.ErrorHandler != nil { mountedErrHandler = subApp.config.ErrorHandler } From 435e64adf2451e5c5fe897956fa18ba2d93668ad Mon Sep 17 00:00:00 2001 From: Caleb Case Date: Mon, 14 Nov 2022 14:34:35 -0500 Subject: [PATCH 5/5] Apply the same trick to customMethod --- app.go | 4 ---- helpers.go | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/app.go b/app.go index b0b78bd618..55896ae7d5 100644 --- a/app.go +++ b/app.go @@ -110,8 +110,6 @@ 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 @@ -562,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)) 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