-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
🚀 [Feature]: middleware/recover: ignore error from panics #2424
Conversation
Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
I understand the security concerns then rather get the 500 and keep the body general "Internal server error" |
500 is not an option as proxy servers will close connection, increasing communication costs The same for default text. Raw English text confuse way more especially when browser/service expect json for example. With backward compatibility such custom code and text can be done with
|
if cfg.SkipResponseError { | ||
return | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems better to add a panic handler to customize the user behavior, the default behavior is to return err.
- refer to cloudwego/hertz@f687dac
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sry, there is already a StackTraceHandler and a handler can be used to do this, but there will be break changes.😞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be ok for me, but with fiber error handler we already have this possibility, because the error which is caught in the middleware is passed on to the error handler and this is responsible for the output or the conversion of the response.
https://docs.gofiber.io/guide/error-handling#custom-error-handler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap though to add recoveryHandler(c *fiber.Ctx, p any) error
but this mean everything else not needed :D
If community agree I can add logic to be the same as https://github.com/grpc-ecosystem/go-grpc-middleware/blob/main/interceptors/recovery/interceptors.go#L29 example
...
if r := recover(); r != nil {
err = recoverFrom(c, cfg, r)
}
...
func recoverFrom(c *fiber.Ctx, cfg Config, p any) error {
if cfg.EnableStackTrace {
cfg.StackTraceHandler(c, r)
}
if cfg.RecoveryHandlerFunc != nil {
return cfg.RecoveryHandlerFunc(c, p)
}
var err error
var ok bool
if err, ok = r.(error); !ok {
// Set error that will call the global error handler
err = fmt.Errorf("%v", r)
}
return err
}
why should this not be an option in my opinion it is an internal server error whether the proxy server closes the connection or not I would also advise against the incorrect use of the stackTraceHandler, because this is designed to write away or reprocess the stack trace and should not be used incorrectly |
Closing as custom response on panic can be done as part |
anyway thanks that you wanted to improve something 🚀 |
Description
Once panic occur there is possibility to recover and continue serving request. Current
recover
middleware also transform panic message into error and pass into response body with status code 500. This can confuse users or disclose secure information.With skipping error message response become good (code 200) and body is empty. Extended with custom
StackTraceHandler
allow to write any response and status code.Type of change
Please delete options that are not relevant.
Checklist: