Skip to content

Commit d05f0ec

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

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* [#2514](https://github.com/ruby-grape/grape/pull/2514): Add rails 8.0 to CI - [@ericproulx](https://github.com/ericproulx).
1212
* [#2516](https://github.com/ruby-grape/grape/pull/2516): Dynamic registration for parsers, formatters, versioners - [@ericproulx](https://github.com/ericproulx).
1313
* [#2518](https://github.com/ruby-grape/grape/pull/2518): Add ruby 3.4 to CI - [@ericproulx](https://github.com/ericproulx).
14+
* [#2528](https://github.com/ruby-grape/grape/pull/2528): Passed status to ErrorFormatter - [@drewnichols](https://github.com/drewnichols).
15+
1416
* Your contribution here.
1517

1618
#### Fixes

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,8 +2772,8 @@ Custom error formatters for existing and additional types can be defined with a
27722772

27732773
```ruby
27742774
class Twitter::API < Grape::API
2775-
error_formatter :txt, ->(message, backtrace, options, env, original_exception) {
2776-
"error: #{message} from #{backtrace}"
2775+
error_formatter :txt, ->(message, backtrace, options, env, original_exception, status) {
2776+
"error: #{message} status: #{status} from #{backtrace}"
27772777
}
27782778
end
27792779
```
@@ -2782,8 +2782,8 @@ You can also use a module or class.
27822782

27832783
```ruby
27842784
module CustomFormatter
2785-
def self.call(message, backtrace, options, env, original_exception)
2786-
{ message: message, backtrace: backtrace }
2785+
def self.call(message, backtrace, options, env, original_exception, status)
2786+
{ message: message, status: status, backtrace: backtrace }
27872787
end
27882788
end
27892789

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) # rubocop:disable Metrics/ParameterLists
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) # rubocop:disable Metrics/ParameterLists
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)