Skip to content

Commit 2833464

Browse files
committed
Add spec to cover all rescue_from :all exception scenarios
Added another spec to demonstrate current behavior when attempting to rescue from a non-StandardError exception. As of now, rescue_from :all only rescues StandardError exceptions, so when a non-StandardError exception is encountered, Grape will not trap the error.
1 parent 5b4c240 commit 2833464

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

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)