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

Return 500 status when an exception is caught in middleware #469

Merged
merged 1 commit into from Jul 6, 2020

Conversation

steventux
Copy link
Contributor

@steventux steventux commented May 21, 2020

If an error occurs in the middleware we should reflect that in the response status instead of echoing the success of the unmodified response.

This will help with response status test assertions. We recently had middleware misconfiguration issues which were not caught by tests expecting a 200 response.

# Do not cache PDFs
headers.delete('ETag')
headers.delete('Cache-Control')
begin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this begin is necessary. We can just catch exceptions in the whole method.

Also, since we are returning an error, can we put the error in the body?

Can we catch just StandardError as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @serene, I've amended as suggested.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@serene without begin the middleware catches the exceptions on the @app.call() above, so it will try to handle exceptions outside of the middleware.

This causes the headers to be nil when exception is triggered, which in turn crashes other middleware down the stack.

If an error occurs in the middleware we should reflect that in the response status.
A real life example of this was adding compression middleware before PDF middleware
which resulted in an exception, tests for a successful response status still passed.
@serene serene added the ready label Jun 8, 2020
@serene serene merged commit 1f8abf9 into pdfkit:master Jul 6, 2020
@serene serene removed the ready label Jul 6, 2020
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.

None yet

3 participants