diff --git a/CHANGELOG.md b/CHANGELOG.md index f1be3b1d96..862a60d541 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/grape/middleware/error.rb b/lib/grape/middleware/error.rb index 20f6a89f35..5a8ba3dafd 100644 --- a/lib/grape/middleware/error.rb +++ b/lib/grape/middleware/error.rb @@ -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) diff --git a/spec/grape/middleware/error_spec.rb b/spec/grape/middleware/error_spec.rb index cdfcc82d17..d056ca7e2a 100644 --- a/spec/grape/middleware/error_spec.rb +++ b/spec/grape/middleware/error_spec.rb @@ -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 '/'