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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for "馃悰 Sub fiber's error handlers not working #1839" and "馃悰 Mount sub fiber app on '/' #1842" #1848

Merged
merged 1 commit into from Apr 5, 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
6 changes: 5 additions & 1 deletion app.go
Expand Up @@ -552,6 +552,10 @@ func (app *App) handleTrustedProxy(ipAddress string) {
func (app *App) Mount(prefix string, fiber *App) Router {
stack := fiber.Stack()
prefix = strings.TrimRight(prefix, "/")
if prefix == "" {
prefix = "/"
}

for m := range stack {
for r := range stack[m] {
route := app.copyRoute(stack[m][r])
Expand Down Expand Up @@ -1075,7 +1079,7 @@ func (app *App) ErrorHandler(ctx *Ctx, err error) error {
)

for prefix, subApp := range app.appList {
if strings.HasPrefix(ctx.path, prefix) && prefix != "" {
if prefix != "" && strings.HasPrefix(ctx.path, prefix) {
parts := len(strings.Split(prefix, "/"))
if mountedPrefixParts <= parts {
mountedErrHandler = subApp.config.ErrorHandler
Expand Down
58 changes: 47 additions & 11 deletions app_test.go
Expand Up @@ -43,6 +43,15 @@ func testStatus200(t *testing.T, app *App, url string, method string) {
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
}

func testErrorResponse(t *testing.T, err error, resp *http.Response, expectedBodyError string) {
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 500, resp.StatusCode, "Status code")

body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, expectedBodyError, string(body), "Response body")
}

func Test_App_MethodNotAllowed(t *testing.T) {
app := New()

Expand Down Expand Up @@ -256,12 +265,26 @@ func Test_App_ErrorHandler_GroupMount(t *testing.T) {
v1.Mount("/john", micro)

resp, err := app.Test(httptest.NewRequest(MethodGet, "/v1/john/doe", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 500, resp.StatusCode, "Status code")
testErrorResponse(t, err, resp, "1: custom error")
}

body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "1: custom error", string(body))
func Test_App_ErrorHandler_GroupMountRootLevel(t *testing.T) {
micro := New(Config{
ErrorHandler: func(c *Ctx, err error) error {
utils.AssertEqual(t, "0: GET error", err.Error())
return c.Status(500).SendString("1: custom error")
},
})
micro.Get("/john/doe", func(c *Ctx) error {
return errors.New("0: GET error")
})

app := New()
v1 := app.Group("/v1")
v1.Mount("/", micro)

resp, err := app.Test(httptest.NewRequest(MethodGet, "/v1/john/doe", nil))
testErrorResponse(t, err, resp, "1: custom error")
}

func Test_App_Nested_Params(t *testing.T) {
Expand Down Expand Up @@ -1603,7 +1626,7 @@ func Test_App_UseMountedErrorHandler(t *testing.T) {

fiber := New(Config{
ErrorHandler: func(ctx *Ctx, err error) error {
return ctx.Status(200).SendString("hi, i'm a custom error")
return ctx.Status(500).SendString("hi, i'm a custom error")
},
})
fiber.Get("/", func(c *Ctx) error {
Expand All @@ -1613,12 +1636,25 @@ func Test_App_UseMountedErrorHandler(t *testing.T) {
app.Mount("/api", fiber)

resp, err := app.Test(httptest.NewRequest(MethodGet, "/api", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
testErrorResponse(t, err, resp, "hi, i'm a custom error")
}

b, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "iotuil.ReadAll()")
utils.AssertEqual(t, "hi, i'm a custom error", string(b), "Response body")
func Test_App_UseMountedErrorHandlerRootLevel(t *testing.T) {
app := New()

fiber := New(Config{
ErrorHandler: func(ctx *Ctx, err error) error {
return ctx.Status(500).SendString("hi, i'm a custom error")
},
})
fiber.Get("/api", func(c *Ctx) error {
return errors.New("something happened")
})

app.Mount("/", fiber)

resp, err := app.Test(httptest.NewRequest(MethodGet, "/api", nil))
testErrorResponse(t, err, resp, "hi, i'm a custom error")
}

func Test_App_UseMountedErrorHandlerForBestPrefixMatch(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion group.go
Expand Up @@ -25,6 +25,10 @@ type Group struct {
func (grp *Group) Mount(prefix string, fiber *App) Router {
stack := fiber.Stack()
groupPath := getGroupPath(grp.Prefix, prefix)
groupPath = strings.TrimRight(groupPath, "/")
if groupPath == "" {
groupPath = "/"
}

for m := range stack {
for r := range stack[m] {
Expand All @@ -34,7 +38,6 @@ func (grp *Group) Mount(prefix string, fiber *App) Router {
}

// Support for configs of mounted-apps and sub-mounted-apps
groupPath = strings.TrimRight(groupPath, "/")
for mountedPrefixes, subApp := range fiber.appList {
grp.app.appList[groupPath+mountedPrefixes] = subApp
subApp.init()
Expand Down