Track Configured Values (#2221)

* WIP: Use Parent Error Handler on Mount

* Add suggested boolean guard

* Move flag to App

* Move to copy of config as configured

* Apply the same trick to customMethod
pull/2224/head
Caleb Case 2022-11-15 06:13:11 -05:00 committed by GitHub
parent 235cd9df82
commit 61b4496067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 6 deletions

14
app.go
View File

@ -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
}
}

View File

@ -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

View File

@ -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()