Skip to content

Commit 27c9926

Browse files
committed
app: Add ParameterMissing exception handling
This way, if a parameter is missing in a request, it is logged as a 400 Bad Request instead of a 500 Internal Server Error.
1 parent 88afa51 commit 27c9926

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

app/controllers/concerns/exception_handling.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ def log_error(error)
2929
render :not_found, status: :not_found, locals: { exception: error }
3030
end
3131

32+
rescue_from ActionController::ParameterMissing do |error|
33+
log_error(error)
34+
render :bad_request, status: :bad_request, locals: { exception: error }
35+
end
36+
3237
rescue_from Error::PatronApiError do |error|
3338
log_error(error)
3439
render :patron_api_error, status: :service_unavailable
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<h1>Bad Request</h1>
2+
3+
<p>Your browser sent a request that the server does not understand.</p>
4+
5+
<% if defined?(exception) && Rails.env.test? %>
6+
<%= render(partial: 'exception_details', locals: { exception: exception }) %>
7+
<% end %>

spec/request/fees_request_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ def base_url_for(user_id = nil)
1212
allow(Rails.application.config).to receive(:alma_api_key).and_return(alma_api_key)
1313
end
1414

15-
it 'throws a ParameterMissing error if request has no jwt' do
16-
expect { get fees_path }.to raise_error(ActionController::ParameterMissing)
15+
it 'shows a Bad Request error if request has no jwt' do
16+
get fees_path
17+
expect(response).to have_http_status(:bad_request)
1718
end
1819

1920
it 'redirects to error page if request has a non-existant alma id' do

spec/request/tind_download_request_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@
114114

115115
describe 'find_collection' do
116116
it 'requires a collection' do
117-
expect { get tind_download_find_collection_path }.to raise_error(ActionController::ParameterMissing)
117+
get tind_download_find_collection_path
118+
expect(response).to have_http_status :bad_request
118119
end
119120

120121
it 'returns the matching collection name list' do

spec/request/validate_proxy_patron_request_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ def base_url_for(user_id)
1616
end
1717

1818
it 'alerts user of invalid parameters' do
19-
expect { post('/validate_proxy_patron') }.to raise_error ActionController::ParameterMissing
19+
post('/validate_proxy_patron')
20+
expect(response).to have_http_status :bad_request
2021
end
2122

2223
it 'alerts user if they failed to authenticate' do

0 commit comments

Comments
 (0)