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

Fix rescue_from with invalid response #2478

Merged
merged 5 commits into from
Jul 23, 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
14 changes: 3 additions & 11 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-06-22 17:26:10 UTC using RuboCop version 1.64.1.
# on 2024-07-23 11:24:53 UTC using RuboCop version 1.64.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -37,12 +37,6 @@ Naming/VariableNumber:
- 'spec/grape/exceptions/validation_errors_spec.rb'
- 'spec/grape/validations_spec.rb'

# Offense count: 2
RSpec/AnyInstance:
Exclude:
- 'spec/grape/api_spec.rb'
- 'spec/grape/middleware/base_spec.rb'

# Offense count: 1
# Configuration parameters: IgnoredMetadata.
RSpec/DescribeClass:
Expand Down Expand Up @@ -140,15 +134,14 @@ RSpec/RepeatedExampleGroupDescription:
- 'spec/grape/util/inheritable_setting_spec.rb'
- 'spec/grape/validations/validators/values_spec.rb'

# Offense count: 5
# Offense count: 4
RSpec/StubbedMock:
Exclude:
- 'spec/grape/dsl/inside_route_spec.rb'
- 'spec/grape/dsl/routing_spec.rb'
- 'spec/grape/middleware/formatter_spec.rb'
- 'spec/grape/parser_spec.rb'

# Offense count: 122
# Offense count: 121
RSpec/SubjectStub:
Exclude:
- 'spec/grape/api_spec.rb'
Expand All @@ -164,7 +157,6 @@ RSpec/SubjectStub:
- 'spec/grape/middleware/formatter_spec.rb'
- 'spec/grape/middleware/globals_spec.rb'
- 'spec/grape/middleware/stack_spec.rb'
- 'spec/grape/parser_spec.rb'

# Offense count: 23
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#### Fixes

* [#2471](https://github.com/ruby-grape/grape/pull/2471): Fix absence of original_exception and/or backtrace even if passed in error! - [@numbata](https://github.com/numbata).
* [#2478](https://github.com/ruby-grape/grape/pull/2478): Fix rescue_from with invalid response - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

### 2.1.3 (2024-07-13)
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def run_rescue_handler(handler, error, endpoint)
elsif response.is_a?(Rack::Response)
response
else
run_rescue_handler(:default_rescue_handler, Grape::Exceptions::InvalidResponse.new, endpoint)
run_rescue_handler(method(:default_rescue_handler), Grape::Exceptions::InvalidResponse.new, endpoint)
end
end

Expand Down
16 changes: 8 additions & 8 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2193,14 +2193,14 @@ def foo
expect(last_response.body).to eq('Formatter Error')
end

it 'uses default_rescue_handler to handle invalid response from rescue_from' do
subject.rescue_from(:all) { 'error' }
subject.get('/') { raise }

expect_any_instance_of(Grape::Middleware::Error).to receive(:default_rescue_handler).and_call_original
get '/'
expect(last_response).to be_server_error
expect(last_response.body).to eql 'Invalid response'
context 'when rescue_from block returns an invalid response' do
it 'returns a formatted response' do
subject.rescue_from(:all) { 'error' }
subject.get('/') { raise }
get '/'
expect(last_response).to be_server_error
expect(last_response.body).to eql 'Invalid response'
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/grape/middleware/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
context 'with patched warnings' do
before do
@warnings = warnings = []
allow_any_instance_of(described_class).to receive(:warn) { |m| warnings << m }
allow(subject).to receive(:warn) { |m| warnings << m }
allow(subject).to receive(:after).and_raise(StandardError)
end

Expand Down