Skip to content

Commit

Permalink
Align error! method signatures across different places. (#2468)
Browse files Browse the repository at this point in the history
* Add spec with calling `error!` helper inside the `rescue_from` block

* Align the signature of Grape::DSL#error! method

* Update CHANGELOG.md
  • Loading branch information
numbata committed Jul 3, 2024
1 parent b47d9ad commit 5affa8f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Fixes

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

### 2.1.2 (2024-06-28)
Expand Down
8 changes: 5 additions & 3 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,14 @@ def configuration
# end user with the specified message.
#
# @param message [String] The message to display.
# @param status [Integer] the HTTP Status Code. Defaults to default_error_status, 500 if not set.
# @param status [Integer] The HTTP Status Code. Defaults to default_error_status, 500 if not set.
# @param additional_headers [Hash] Addtional headers for the response.
def error!(message, status = nil, additional_headers = nil)
# @param backtrace [Array<String>] The backtrace of the exception that caused the error.
# @param original_exception [Exception] The original exception that caused the error.
def error!(message, status = nil, additional_headers = nil, backtrace = nil, original_exception = nil)
status = self.status(status || namespace_inheritable(:default_error_status))
headers = additional_headers.present? ? header.merge(additional_headers) : header
throw :error, message: message, status: status, headers: headers
throw :error, message: message, status: status, headers: headers, backtrace: backtrace, original_exception: original_exception
end

# Creates a Rack response based on the provided message, status, and headers.
Expand Down
12 changes: 12 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2532,6 +2532,18 @@ def self.call(message, _backtrace, _options, _env, _original_exception)
get '/exception'
expect(last_response.body).to eq('message: rain! @backtrace')
end

it 'returns a modified error with a custom error format' do
subject.rescue_from :all, backtrace: true do |e|
error!('raining dogs and cats', 418, {}, e.backtrace, e)
end
subject.error_formatter :txt, with: custom_error_formatter
subject.get '/exception' do
raise 'rain!'
end
get '/exception'
expect(last_response.body).to eq('message: raining dogs and cats @backtrace')
end
end

it 'rescues all errors and return :json' do
Expand Down

0 comments on commit 5affa8f

Please sign in to comment.