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 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ type App struct {
customMethod bool
efectn marked this conversation as resolved.
Show resolved Hide resolved
// 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 +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'.")
Expand Down Expand Up @@ -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.configured.ErrorHandler != nil {
mountedErrHandler = subApp.config.ErrorHandler
}

mountedPrefixParts = parts
}
}
Expand Down
18 changes: 18 additions & 0 deletions mount_test.go
Original file line number Diff line number Diff line change
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