Skip to content

Commit

Permalink
Error middleware support using rack util's symbols as status (#2274)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvCW authored Jul 31, 2022
1 parent 9bce86f commit 362724d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [#2271](https://github.com/ruby-grape/grape/pull/2271): Fixed validation regression on Numeric type introduced in 1.3 - [@vasfed](https://github.com/Vasfed).
* [#2267](https://github.com/ruby-grape/grape/pull/2267): Standardized English error messages - [@dblock](https://github.com/dblock).
* [#2272](https://github.com/ruby-grape/grape/pull/2272): Added error on param init when provided type does not have `[]` coercion method, previously validation silently failed for any value - [@vasfed](https://github.com/Vasfed).
* [#2274](https://github.com/ruby-grape/grape/pull/2274): Error middleware support using rack util's symbols as status - [@dhruvCW](https://github.com/dhruvCW).
* Your contribution here.

#### Fixes
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def error_response(error = {})

def rack_response(message, status = options[:default_status], headers = { Grape::Http::Headers::CONTENT_TYPE => content_type })
message = ERB::Util.html_escape(message) if headers[Grape::Http::Headers::CONTENT_TYPE] == TEXT_HTML
Rack::Response.new([message], status, headers)
Rack::Response.new([message], Rack::Utils.status_code(status), headers)
end

def format_message(message, backtrace, original_exception = nil)
Expand Down
6 changes: 6 additions & 0 deletions spec/grape/middleware/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def app
expect(last_response.status).to eq(410)
end

it 'sets the status code based on the rack util status code symbol' do
ErrorSpec::ErrApp.error = { status: :gone }
get '/'
expect(last_response.status).to eq(410)
end

it 'sets the error message appropriately' do
ErrorSpec::ErrApp.error = { message: 'Awesome stuff.' }
get '/'
Expand Down

0 comments on commit 362724d

Please sign in to comment.