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

Error handling interfere with other Error handling middleware #35

Open
philipjoh opened this issue Jun 10, 2024 · 1 comment
Open

Error handling interfere with other Error handling middleware #35

philipjoh opened this issue Jun 10, 2024 · 1 comment

Comments

@philipjoh
Copy link

Hello!

In my project we have our own global error handler that produce a custom http response, but we noticed that the output in the http response was printed 2 times because of this call:

c.Error(err)

Reading the doc of the Error function says:

A side-effect of calling global error handler is that now Response has been committed (sent to the client) and middlewares up in chain can not change Response status code or Response body anymore.

Avoid using this method in handlers as no middleware will be able to effectively handle errors after that.

So would it make sense to just remove this call from this middleware? And perhaps instead just return the err in the end of this function? As the purpose of this library isn't to handle the error, just to log it?

@philipjoh philipjoh changed the title Error handling interferce with our own custom error handler Error handling interferce with other Error handling middleware Jun 10, 2024
@philipjoh philipjoh changed the title Error handling interferce with other Error handling middleware Error handling interfere with other Error handling middleware Jun 10, 2024
@mgnsk
Copy link

mgnsk commented Feb 7, 2025

I have a similar question.

When I use the Recover middleware and this middleware together, the log output is different depending on the order of middlewares:

When Recover middleware is before slogecho, slogecho never logs the panic, only Recover does.

e := echo.New()
e.Use(
  middleware.Recover(),
  slogecho.New(),
)

When slogecho is before Recover, both Recover logs the panic (with stacktrace) and slogecho logs the panic (without stacktrace).
vs

e := echo.New()
e.Use(
  slogecho.New(),
  middleware.Recover(),
)

I currently prefer the second option where both log. Recover logs the stacktrace and slogecho just the request.

Maybe one of the possible solutions would be to make slogecho handle panics so that the Recover middleware could be dropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants