Skip to content

Commit

Permalink
added http status to the format_error parameter list making it access…
Browse files Browse the repository at this point in the history
…ible to ErrorFormatter children
  • Loading branch information
drewnichols committed Jan 28, 2025
1 parent 5ce44de commit cb87e68
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/grape/error_formatter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit cb87e68

Please sign in to comment.