-
Notifications
You must be signed in to change notification settings - Fork 890
feat: add panic recovery middleware #3687
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
Conversation
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.
Thanks for implementing this! ❤️
// StatusWriter intercepts the status of the request and the response body up | ||
// to maxBodySize if Status >= 400. It is guaranteed to be the ResponseWriter | ||
// directly downstream from Middleware. | ||
type StatusWriter struct { |
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.
Suggestion (non-blocking): I think it could be beneficial to make this a package-private type and expose the fields via functions.
Benefit is that a caller can't access the underlying ResponseWriter (bypassing StatusWriter), but can still test for the values via if s, ok := rw.(interface{Status() int}); ok { s.Status() }
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.
I'm not sure I'm following. Do you just want to create a StatusWriter
interface and unexport the current struct? Alternatively we could simply unexport the ResponseWriter
on the struct.
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.
Ah, sorry it was unclear, I was suggesting two changes:
- Unexport
ResponseWriter
=>responseWriter
- Change fields from
Status
=>func(*statusWriter) Status() int
Number 1 was the main thing (to protect from direct ResponseWriter access), number 2 was a little extra which would allow the values to be inspected outside the package, if the need ever arises. But not necessary by any means.
In the case of 2, my example of if s, ok := rw.(interface{Status() int}); ok { s.Status() }
was hinting at that we don't need to export anything from this package. If that need ever arises (inspect value outside package), it could be done with an inline interface.
I could've omitted that part though, I don't think it will be needed. 😄
const maxBodySize = 4096 | ||
|
||
if !w.wroteHeader { | ||
w.Status = http.StatusOK |
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.
Technically this will be true once we hit Write
below, a later call to WriteHeader
might overwrite the actual status otherwise.
w.Status = http.StatusOK | |
w.Status = http.StatusOK | |
w.wroteHeader = true |
fixes #2338
Also adds our http logger from v1.
Panic:
Logger: