Skip to content

Commit 838c75e

Browse files
Fix rescue_from with invalid response (#2478)
* Remove expect_any_instance_of(...) Wrap `default_rescue_handler` with `method` * Add CHANGELOG entry * Fix RSpec/AnyInstance Regenerate Rubocop's todo * Update spec wording * Update spec/grape/api_spec.rb Co-authored-by: Manuel Jacob <[email protected]> --------- Co-authored-by: Manuel Jacob <[email protected]>
1 parent fb67ea9 commit 838c75e

File tree

5 files changed

+14
-21
lines changed

5 files changed

+14
-21
lines changed

.rubocop_todo.yml

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2024-06-22 17:26:10 UTC using RuboCop version 1.64.1.
3+
# on 2024-07-23 11:24:53 UTC using RuboCop version 1.64.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -37,12 +37,6 @@ Naming/VariableNumber:
3737
- 'spec/grape/exceptions/validation_errors_spec.rb'
3838
- 'spec/grape/validations_spec.rb'
3939

40-
# Offense count: 2
41-
RSpec/AnyInstance:
42-
Exclude:
43-
- 'spec/grape/api_spec.rb'
44-
- 'spec/grape/middleware/base_spec.rb'
45-
4640
# Offense count: 1
4741
# Configuration parameters: IgnoredMetadata.
4842
RSpec/DescribeClass:
@@ -140,15 +134,14 @@ RSpec/RepeatedExampleGroupDescription:
140134
- 'spec/grape/util/inheritable_setting_spec.rb'
141135
- 'spec/grape/validations/validators/values_spec.rb'
142136

143-
# Offense count: 5
137+
# Offense count: 4
144138
RSpec/StubbedMock:
145139
Exclude:
146140
- 'spec/grape/dsl/inside_route_spec.rb'
147141
- 'spec/grape/dsl/routing_spec.rb'
148142
- 'spec/grape/middleware/formatter_spec.rb'
149-
- 'spec/grape/parser_spec.rb'
150143

151-
# Offense count: 122
144+
# Offense count: 121
152145
RSpec/SubjectStub:
153146
Exclude:
154147
- 'spec/grape/api_spec.rb'
@@ -164,7 +157,6 @@ RSpec/SubjectStub:
164157
- 'spec/grape/middleware/formatter_spec.rb'
165158
- 'spec/grape/middleware/globals_spec.rb'
166159
- 'spec/grape/middleware/stack_spec.rb'
167-
- 'spec/grape/parser_spec.rb'
168160

169161
# Offense count: 23
170162
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.

CHANGELOG.md

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

1010
* [#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).
11+
* [#2478](https://github.com/ruby-grape/grape/pull/2478): Fix rescue_from with invalid response - [@ericproulx](https://github.com/ericproulx).
1112
* Your contribution here.
1213

1314
### 2.1.3 (2024-07-13)

lib/grape/middleware/error.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def run_rescue_handler(handler, error, endpoint)
125125
elsif response.is_a?(Rack::Response)
126126
response
127127
else
128-
run_rescue_handler(:default_rescue_handler, Grape::Exceptions::InvalidResponse.new, endpoint)
128+
run_rescue_handler(method(:default_rescue_handler), Grape::Exceptions::InvalidResponse.new, endpoint)
129129
end
130130
end
131131

spec/grape/api_spec.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -2193,14 +2193,14 @@ def foo
21932193
expect(last_response.body).to eq('Formatter Error')
21942194
end
21952195

2196-
it 'uses default_rescue_handler to handle invalid response from rescue_from' do
2197-
subject.rescue_from(:all) { 'error' }
2198-
subject.get('/') { raise }
2199-
2200-
expect_any_instance_of(Grape::Middleware::Error).to receive(:default_rescue_handler).and_call_original
2201-
get '/'
2202-
expect(last_response).to be_server_error
2203-
expect(last_response.body).to eql 'Invalid response'
2196+
context 'when rescue_from block returns an invalid response' do
2197+
it 'returns a formatted response' do
2198+
subject.rescue_from(:all) { 'error' }
2199+
subject.get('/') { raise }
2200+
get '/'
2201+
expect(last_response).to be_server_error
2202+
expect(last_response.body).to eql 'Invalid response'
2203+
end
22042204
end
22052205
end
22062206

spec/grape/middleware/base_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
context 'with patched warnings' do
5858
before do
5959
@warnings = warnings = []
60-
allow_any_instance_of(described_class).to receive(:warn) { |m| warnings << m }
60+
allow(subject).to receive(:warn) { |m| warnings << m }
6161
allow(subject).to receive(:after).and_raise(StandardError)
6262
end
6363

0 commit comments

Comments
 (0)