Skip to content

Commit

Permalink
Track Configured Values (#2221)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
calebcase committed Nov 15, 2022
1 parent 235cd9d commit 61b4496
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
14 changes: 9 additions & 5 deletions app.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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'.")
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion helpers.go
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions mount_test.go
Expand Up @@ -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()

Expand Down

1 comment on commit 61b4496

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 61b4496 Previous: 235cd9d Ratio
Benchmark_AcquireCtx 1635 ns/op 1568 B/op 5 allocs/op 755 ns/op 1568 B/op 5 allocs/op 2.17

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.