Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align error! method signatures across different places. #2468

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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