Skip to content

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

Merged
merged 7 commits into from
Aug 29, 2022
Merged

feat: add panic recovery middleware #3687

merged 7 commits into from
Aug 29, 2022

Conversation

sreya
Copy link
Collaborator

@sreya sreya commented Aug 24, 2022

fixes #2338

Also adds our http logger from v1.

Panic:

image

Logger:

image

@sreya sreya requested a review from coadler August 24, 2022 22:17
Copy link
Member

@mafredri mafredri left a 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 {
Copy link
Member

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() }

Copy link
Collaborator Author

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.

Copy link
Member

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:

  1. Unexport ResponseWriter => responseWriter
  2. 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
Copy link
Member

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.

Suggested change
w.Status = http.StatusOK
w.Status = http.StatusOK
w.wroteHeader = true

@sreya sreya requested a review from mafredri August 25, 2022 22:32
@sreya sreya merged commit 053fe6f into main Aug 29, 2022
@sreya sreya deleted the jon/fixpanic branch August 29, 2022 22:00
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

Successfully merging this pull request may close these issues.

Panics in HTTP handlers are not logged
3 participants