-
Notifications
You must be signed in to change notification settings - Fork 10
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
Double running of echo.HTTPErrorHandler #15
Comments
Hey, thanks for bringing it up. |
I've just hit this exact same issue, and can confirm that it does not happen with the standard i.e. this causes the issue:
But this doesn't:
|
Hey guys, sorry for such a late reply. I've been pretty busy lately. |
Can anyone provide an example of their handlers that causes this behavior? |
I ended up using echo logger functionality since i can provide log function and the middleware stuff is handled by echo. so in echo i setup: where log.Logger is zerolog instance and the middleware is just the config with value function for the
|
I am adding some context to if err = next(c); err != nil {
c.Error(err)
} Calling a) make sure that your error handler has this as first condition if c.Response().Committed {
return
} this will make sure that Response that has already been sent to the client (already executed through error handler) is not executed twice. This case you can make sure that your middleware can call Note: this has its shortcomings - for example if you successfully write response to the client and some middleware up in handler chains errors out - you will not catch that as your error handler acts only for cases when you are able to write to the client. Think conceptionally - what purpose your error handler serves - is it to write error responses to client or log+? errors. b) call basically - use this middleware as very first middleware. Note: set panic recovering middleware after that middleware so you are actually executing return parts. For example Echo own e.Use(lecho.Middleware(lecho.Config{
Logger: logger,
}))
e.Use(middleware.Recover()) // <-- after logger so panics are converted to errors and returned to logger middleware Reference: labstack/echo#2341 this exaple of Echo PR that addresses similar issues with |
I decided to get rid of this call altogether. To me, it seems like a violation of the single responsibility principle. |
This is OK. It would be probably better to add comment somewhere that middleware probably reports status incorrectly on error. Line 142 in 967237f
In that case it is probably 200 (default status) and actual status code is decided when request it is run through error handler.
|
Good catch. Maybe I could add an optional error handler and pass Echo context into it, so if for someone it's breaking change, they could easily revert the old behavior. |
Okay, I added a new flag, pretty much the same one that Echo has, to enable-disable this 'feature'. |
I'm experiencing double calling of HTTPErrorHandler by lecho middleware logger.
First it's called if there was error during request processing, immediately when request is returned from middleware chain:
lecho/middleware.go
Lines 84 to 86 in 6b6fa8f
Next request log is produced and sent, but the middleware returns the same error again up:
lecho/middleware.go
Line 126 in 6b6fa8f
This causes the error to be propagated up the chain and is again sent to HTTPErrorHandler function by echo itself:
https://github.com/labstack/echo/blob/4a1ccdfdc520eb90573a97a7d04fd9fc300c1629/echo.go#L619-L631
The "native" echo Logger middleware does not do this, the error is consumed by calling handler immediately (the same as lecho) and then error sent up the middleware chain is actual result (error) of writing the log.
Do I understand something wrongly (ie is this expected behavior and i'm supposed to somehow catch the error) or is this actual bug?
The text was updated successfully, but these errors were encountered: