Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track Configured Values #2221

Merged
merged 5 commits into from Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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