Skip to content

🐛 [Bug]: route (and Route()) property from fiber context returning nil when called after body limit error #2832

@gabrielmarinhoindico

Description

@gabrielmarinhoindico

Bug Description

In my application, I create an instance of a fiber server and fill it with some configurations. After init it I pass the routes and middlewares created.
I have a function for the Error Handler and with any other error when I use context.AllParams() inside it I get the proper return but with this specific error (body limit) the context.route (private field used internally by AllParams) or the context.Route() doesn't return the values, the first one returns nil and gets a panic when called dot params inside the AllParams method. The second one returns a struct with empty values.
All the other methods are getting the proper return (ctx.Path, ctx.Queries, etc).

Captura de tela de 2024-02-05 10-00-31

image

I guess that the problem is the body limit error is thrown before getting to the routes that fill that variable internally.

How to Reproduce

Steps to reproduce the behavior:

  1. Create a struct to the application:
type Application struct {
	Server *fiber.App
}

var Data *Application
  1. Create a fiber server with the following config:
	appinstance.Data = &appinstance.Application{
		Server: fiber.New(fiber.Config{
			ServerHeader: "Some Header name Here",
			ErrorHandler: customErrorHandler,
			Prefork:      false,
		}),
	}

function to errorHandler:

func customErrorHandler(ctx *fiber.Ctx, err error) error {
   
     allParams := ctx.AllParams()
     // panic will happen here getting the params because the internal property route is nil


	logging.Log(
		&entity.LogDetails{
			Message:  message,
			Method:   ctx.Method(),
			Reason:   err.Error(),
			RemoteIP: ctx.IP(),
			Request: map[string]interface{}{
				"body":       string(ctx.BodyRaw()),
				"query":      ctx.Queries(),
				"url_params": ,
			},
			StatusCode: code,
			URLpath:    ctx.Path(),
		},
		"critical",
		nil,
	)

	return nil
}
  1. create a patch route that receives any path param;
  2. Listen the server and run it;
  3. make the request sending a file that it's higher than the fiber body limit (the default is 4MB +/- (4 * 1024 * 1024)

Expected Behavior

I expect that the AllParams return the proper value, or return an empty struct at least, because the AllParams don't expect to return an error, but if it can happen would be nice to return it in a v3 of fiber maybe?

Fiber Version

v2.49.2

Code Snippet (optional)

No response

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions