diff --git a/lib/grape/error_formatter/base.rb b/lib/grape/error_formatter/base.rb index f2cf223c6..9cef2ead6 100644 --- a/lib/grape/error_formatter/base.rb +++ b/lib/grape/error_formatter/base.rb @@ -4,7 +4,7 @@ module Grape module ErrorFormatter class Base class << self - def call(message, backtrace, options = {}, env = nil, original_exception = nil) + def call(message, backtrace, options = {}, env = nil, original_exception = nil, _status = nil) merge_backtrace = backtrace.present? && options.dig(:rescue_options, :backtrace) merge_original_exception = original_exception && options.dig(:rescue_options, :original_exception) diff --git a/lib/grape/middleware/error.rb b/lib/grape/middleware/error.rb index f201ee8ef..af42ef8c4 100644 --- a/lib/grape/middleware/error.rb +++ b/lib/grape/middleware/error.rb @@ -43,10 +43,10 @@ def rack_response(status, headers, message) Rack::Response.new(Array.wrap(message), Rack::Utils.status_code(status), Grape::Util::Header.new.merge(headers)) end - def format_message(message, backtrace, original_exception = nil) + def format_message(message, backtrace, original_exception = nil, status = nil) format = env[Grape::Env::API_FORMAT] || options[:format] formatter = Grape::ErrorFormatter.formatter_for(format, options[:error_formatters], options[:default_error_formatter]) - return formatter.call(message, backtrace, options, env, original_exception) if formatter + return formatter.call(message, backtrace, options, env, original_exception, status) if formatter throw :error, status: 406, @@ -71,7 +71,7 @@ def error_response(error = {}) end backtrace = error[:backtrace] || error[:original_exception]&.backtrace || [] original_exception = error.is_a?(Exception) ? error : error[:original_exception] || nil - rack_response(status, headers, format_message(message, backtrace, original_exception)) + rack_response(status, headers, format_message(message, backtrace, original_exception, status)) end def default_rescue_handler(exception) @@ -132,7 +132,7 @@ def run_rescue_handler(handler, error, endpoint) def error!(message, status = options[:default_status], headers = {}, backtrace = [], original_exception = nil) rack_response( status, headers.reverse_merge(Rack::CONTENT_TYPE => content_type), - format_message(message, backtrace, original_exception) + format_message(message, backtrace, original_exception, status) ) end diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 20193ccb3..6c410af09 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -2509,8 +2509,8 @@ def rescue_all_errors context 'class' do let(:custom_error_formatter) do Class.new do - def self.call(message, _backtrace, _options, _env, _original_exception) - "message: #{message} @backtrace" + def self.call(message, _backtrace, _options, _env, _original_exception, status) + "message: #{message} status: #{status} @backtrace" end end end @@ -2521,7 +2521,7 @@ def self.call(message, _backtrace, _options, _env, _original_exception) subject.get('/exception') { raise 'rain!' } get '/exception' - expect(last_response.body).to eq('message: rain! @backtrace') + expect(last_response.body).to eq('message: rain! status: 500 @backtrace') end it 'returns a custom error format (using keyword :with)' do @@ -2530,7 +2530,7 @@ def self.call(message, _backtrace, _options, _env, _original_exception) subject.get('/exception') { raise 'rain!' } get '/exception' - expect(last_response.body).to eq('message: rain! @backtrace') + expect(last_response.body).to eq('message: rain! status: 500 @backtrace') end it 'returns a modified error with a custom error format' do @@ -2542,7 +2542,7 @@ def self.call(message, _backtrace, _options, _env, _original_exception) raise 'rain!' end get '/exception' - expect(last_response.body).to eq('message: raining dogs and cats @backtrace') + expect(last_response.body).to eq('message: raining dogs and cats status: 418 @backtrace') end end