Skip to content

Commit 5affa8f

Browse files
authored
Align error! method signatures across different places. (#2468)
* Add spec with calling `error!` helper inside the `rescue_from` block * Align the signature of Grape::DSL#error! method * Update CHANGELOG.md
1 parent b47d9ad commit 5affa8f

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* [#2467](https://github.com/ruby-grape/grape/pull/2467): Fix repo coverage - [@ericproulx](https://github.com/ericproulx).
10+
* [#2468](https://github.com/ruby-grape/grape/pull/2468): Align `error!` method signatures across different places - [@numbata](https://github.com/numbata).
1011
* Your contribution here.
1112

1213
### 2.1.2 (2024-06-28)

lib/grape/dsl/inside_route.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,14 @@ def configuration
163163
# end user with the specified message.
164164
#
165165
# @param message [String] The message to display.
166-
# @param status [Integer] the HTTP Status Code. Defaults to default_error_status, 500 if not set.
166+
# @param status [Integer] The HTTP Status Code. Defaults to default_error_status, 500 if not set.
167167
# @param additional_headers [Hash] Addtional headers for the response.
168-
def error!(message, status = nil, additional_headers = nil)
168+
# @param backtrace [Array<String>] The backtrace of the exception that caused the error.
169+
# @param original_exception [Exception] The original exception that caused the error.
170+
def error!(message, status = nil, additional_headers = nil, backtrace = nil, original_exception = nil)
169171
status = self.status(status || namespace_inheritable(:default_error_status))
170172
headers = additional_headers.present? ? header.merge(additional_headers) : header
171-
throw :error, message: message, status: status, headers: headers
173+
throw :error, message: message, status: status, headers: headers, backtrace: backtrace, original_exception: original_exception
172174
end
173175

174176
# Creates a Rack response based on the provided message, status, and headers.

spec/grape/api_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,18 @@ def self.call(message, _backtrace, _options, _env, _original_exception)
25322532
get '/exception'
25332533
expect(last_response.body).to eq('message: rain! @backtrace')
25342534
end
2535+
2536+
it 'returns a modified error with a custom error format' do
2537+
subject.rescue_from :all, backtrace: true do |e|
2538+
error!('raining dogs and cats', 418, {}, e.backtrace, e)
2539+
end
2540+
subject.error_formatter :txt, with: custom_error_formatter
2541+
subject.get '/exception' do
2542+
raise 'rain!'
2543+
end
2544+
get '/exception'
2545+
expect(last_response.body).to eq('message: raining dogs and cats @backtrace')
2546+
end
25352547
end
25362548

25372549
it 'rescues all errors and return :json' do

0 commit comments

Comments
 (0)