Skip to content

Commit

Permalink
[#1719] Fix missing headers before error! call
Browse files Browse the repository at this point in the history
  • Loading branch information
anaumov committed Mar 12, 2019
1 parent b79bd9c commit 081043b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Your contribution here.
* [#1864](https://github.com/ruby-grape/grape/pull/1864): Adds `finally` on the API - [@myxoh](https://github.com/myxoh).
* [#1869](https://github.com/ruby-grape/grape/pull/1869): Fix issue with empty headers after `error!` method call - [@anaumov](https://github.com/anaumov).

#### Fixes

Expand Down
3 changes: 2 additions & 1 deletion lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ def version
#
# @param message [String] The message to display.
# @param status [Integer] the HTTP Status Code. Defaults to default_error_status, 500 if not set.
def error!(message, status = nil, headers = nil)
def error!(message, status = nil, additional_headers = nil)
self.status(status || namespace_inheritable(:default_error_status))
headers = additional_headers.present? ? header.merge(additional_headers) : header
throw :error, message: message, status: self.status, headers: headers
end

Expand Down
11 changes: 11 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,17 @@ def app
expect(last_response.headers['X-Custom']).to eq('value')
end

it 'returns headers set before error!' do
subject.get '/hey' do
header 'X-Test', 'sample'
error!({ 'dude' => 'rad' }, 403)
end

get '/hey.json'
expect(last_response.status).to eq(403)
expect(last_response.headers['X-Test']).to eq('sample')
end

it 'sets the status code for the endpoint' do
memoized_endpoint = nil

Expand Down

0 comments on commit 081043b

Please sign in to comment.