From 5affa8f17ed25787f4b2d0d54d6310a691b778ee Mon Sep 17 00:00:00 2001 From: Andrei Subbota Date: Wed, 3 Jul 2024 17:52:50 +0200 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + lib/grape/dsl/inside_route.rb | 8 +++++--- spec/grape/api_spec.rb | 12 ++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46defa2676..abc7852746 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/grape/dsl/inside_route.rb b/lib/grape/dsl/inside_route.rb index c73eb7c773..320b45a6d4 100644 --- a/lib/grape/dsl/inside_route.rb +++ b/lib/grape/dsl/inside_route.rb @@ -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] 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. diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index dd280c9a56..ca26b812a6 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -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