Skip to content

Commit fda88b5

Browse files
authored
Merge pull request #1725 from Jelkster/rescue-all-doc-fix
`rescue_from :all` documentation fix
2 parents 8395a41 + 69e2109 commit fda88b5

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [#1710](https://github.com/ruby-grape/grape/pull/1710): Fix wrong transformation of empty Array in declared params - [@pablonahuelgomez](https://github.com/pablonahuelgomez).
1313
* [#1722](https://github.com/ruby-grape/grape/pull/1722): Fix catch-all hiding multiple versions of an endpoint after the first definition - [@zherr](https://github.com/zherr).
1414
* [#1724](https://github.com/ruby-grape/grape/pull/1724): Optional nested array validation - [@ericproulx](https://github.com/ericproulx).
15+
* [#1725](https://github.com/ruby-grape/grape/pull/1725): Fix `rescue_from :all` documentation - [@Jelkster](https://github.com/Jelkster).
1516
* Your contribution here.
1617

1718
### 1.0.1 (9/8/2017)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,7 +2071,7 @@ literally accepts every request.
20712071

20722072
## Exception Handling
20732073

2074-
Grape can be told to rescue all exceptions and return them in the API format.
2074+
Grape can be told to rescue all `StandardError` exceptions and return them in the API format.
20752075

20762076
```ruby
20772077
class Twitter::API < Grape::API
@@ -2177,8 +2177,8 @@ class Twitter::API < Grape::API
21772177
error!("ArgumentError: #{e.message}")
21782178
end
21792179

2180-
rescue_from NotImplementedError do |e|
2181-
error!("NotImplementedError: #{e.message}")
2180+
rescue_from NoMethodError do |e|
2181+
error!("NoMethodError: #{e.message}")
21822182
end
21832183
end
21842184
```

spec/grape/middleware/exception_spec.rb

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ def call(_env)
1111
end
1212
end
1313

14+
# raises a non-StandardError (ScriptError) exception
15+
class OtherExceptionApp
16+
class << self
17+
def call(_env)
18+
raise NotImplementedError, 'snow!'
19+
end
20+
end
21+
end
22+
1423
# raises a hash error
1524
class ErrorHashApp
1625
class << self
@@ -68,20 +77,35 @@ def app
6877
end
6978

7079
context 'with rescue_all' do
71-
subject do
72-
Rack::Builder.app do
73-
use Spec::Support::EndpointFaker
74-
use Grape::Middleware::Error, rescue_all: true
75-
run ExceptionSpec::ExceptionApp
80+
context 'StandardError exception' do
81+
subject do
82+
Rack::Builder.app do
83+
use Spec::Support::EndpointFaker
84+
use Grape::Middleware::Error, rescue_all: true
85+
run ExceptionSpec::ExceptionApp
86+
end
87+
end
88+
it 'sets the message appropriately' do
89+
get '/'
90+
expect(last_response.body).to eq('rain!')
91+
end
92+
it 'defaults to a 500 status' do
93+
get '/'
94+
expect(last_response.status).to eq(500)
7695
end
7796
end
78-
it 'sets the message appropriately' do
79-
get '/'
80-
expect(last_response.body).to eq('rain!')
81-
end
82-
it 'defaults to a 500 status' do
83-
get '/'
84-
expect(last_response.status).to eq(500)
97+
98+
context 'Non-StandardError exception' do
99+
subject do
100+
Rack::Builder.app do
101+
use Spec::Support::EndpointFaker
102+
use Grape::Middleware::Error, rescue_all: true
103+
run ExceptionSpec::OtherExceptionApp
104+
end
105+
end
106+
it 'does not trap errors other than StandardError' do
107+
expect { get '/' }.to raise_error(NotImplementedError, 'snow!')
108+
end
85109
end
86110
end
87111

0 commit comments

Comments
 (0)