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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/pdfkit/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def _call(env)
headers['Content-Disposition'] = @conditions[:disposition] || 'inline'
end

[status, headers, response]

rescue StandardError => e
status = 500
response = [e.message]

[status, headers, response]
end

Expand Down
10 changes: 10 additions & 0 deletions spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,16 @@ def mock_app(options = {}, conditions = {}, custom_headers = {})
end
end
end

describe "error handling" do
specify do
mock_app
allow(PDFKit).to receive(:new).and_raise(StandardError.new("Something went wrong"))
get 'http://www.example.org/public/test.pdf'
expect(last_response.status).to eq(500)
expect(last_response.body).to eq("Something went wrong")
end
end
end

describe "remove .pdf from PATH_INFO and REQUEST_URI" do
Expand Down