Skip to content

Commit e8f0495

Browse files
committed
added http status to the format_error parameter list making it accessible to ErrorFormatter children
1 parent 5ce44de commit e8f0495

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

lib/grape/error_formatter/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Grape
44
module ErrorFormatter
55
class Base
66
class << self
7-
def call(message, backtrace, options = {}, env = nil, original_exception = nil)
7+
def call(message, backtrace, options = {}, env = nil, original_exception = nil, status = nil)
88
merge_backtrace = backtrace.present? && options.dig(:rescue_options, :backtrace)
99
merge_original_exception = original_exception && options.dig(:rescue_options, :original_exception)
1010

lib/grape/middleware/error.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ def rack_response(status, headers, message)
4343
Rack::Response.new(Array.wrap(message), Rack::Utils.status_code(status), Grape::Util::Header.new.merge(headers))
4444
end
4545

46-
def format_message(message, backtrace, original_exception = nil)
46+
def format_message(message, backtrace, original_exception = nil, status = nil)
4747
format = env[Grape::Env::API_FORMAT] || options[:format]
4848
formatter = Grape::ErrorFormatter.formatter_for(format, options[:error_formatters], options[:default_error_formatter])
49-
return formatter.call(message, backtrace, options, env, original_exception) if formatter
49+
return formatter.call(message, backtrace, options, env, original_exception, status) if formatter
5050

5151
throw :error,
5252
status: 406,
@@ -71,7 +71,7 @@ def error_response(error = {})
7171
end
7272
backtrace = error[:backtrace] || error[:original_exception]&.backtrace || []
7373
original_exception = error.is_a?(Exception) ? error : error[:original_exception] || nil
74-
rack_response(status, headers, format_message(message, backtrace, original_exception))
74+
rack_response(status, headers, format_message(message, backtrace, original_exception, status))
7575
end
7676

7777
def default_rescue_handler(exception)
@@ -132,7 +132,7 @@ def run_rescue_handler(handler, error, endpoint)
132132
def error!(message, status = options[:default_status], headers = {}, backtrace = [], original_exception = nil)
133133
rack_response(
134134
status, headers.reverse_merge(Rack::CONTENT_TYPE => content_type),
135-
format_message(message, backtrace, original_exception)
135+
format_message(message, backtrace, original_exception, status)
136136
)
137137
end
138138

spec/grape/api_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,8 +2509,8 @@ def rescue_all_errors
25092509
context 'class' do
25102510
let(:custom_error_formatter) do
25112511
Class.new do
2512-
def self.call(message, _backtrace, _options, _env, _original_exception)
2513-
"message: #{message} @backtrace"
2512+
def self.call(message, _backtrace, _options, _env, _original_exception, status)
2513+
"message: #{message} status: #{status} @backtrace"
25142514
end
25152515
end
25162516
end
@@ -2521,7 +2521,7 @@ def self.call(message, _backtrace, _options, _env, _original_exception)
25212521
subject.get('/exception') { raise 'rain!' }
25222522

25232523
get '/exception'
2524-
expect(last_response.body).to eq('message: rain! @backtrace')
2524+
expect(last_response.body).to eq('message: rain! status: 500 @backtrace')
25252525
end
25262526

25272527
it 'returns a custom error format (using keyword :with)' do
@@ -2530,7 +2530,7 @@ def self.call(message, _backtrace, _options, _env, _original_exception)
25302530
subject.get('/exception') { raise 'rain!' }
25312531

25322532
get '/exception'
2533-
expect(last_response.body).to eq('message: rain! @backtrace')
2533+
expect(last_response.body).to eq('message: rain! status: 500 @backtrace')
25342534
end
25352535

25362536
it 'returns a modified error with a custom error format' do
@@ -2542,7 +2542,7 @@ def self.call(message, _backtrace, _options, _env, _original_exception)
25422542
raise 'rain!'
25432543
end
25442544
get '/exception'
2545-
expect(last_response.body).to eq('message: raining dogs and cats @backtrace')
2545+
expect(last_response.body).to eq('message: raining dogs and cats status: 418 @backtrace')
25462546
end
25472547
end
25482548

0 commit comments

Comments
 (0)