Skip to content

Commit

Permalink
Add spec to cover all rescue_from :all exception scenarios
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jelkster committed Dec 31, 2017
1 parent 5b4c240 commit 2833464
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions spec/grape/middleware/exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ def call(_env)
end
end

# raises a non-StandardError (ScriptError) exception
class OtherExceptionApp
class << self
def call(_env)
raise NotImplementedError, 'snow!'
end
end
end

# raises a hash error
class ErrorHashApp
class << self
Expand Down Expand Up @@ -68,20 +77,35 @@ def app
end

context 'with rescue_all' do
subject do
Rack::Builder.app do
use Spec::Support::EndpointFaker
use Grape::Middleware::Error, rescue_all: true
run ExceptionSpec::ExceptionApp
context 'StandardError exception' do
subject do
Rack::Builder.app do
use Spec::Support::EndpointFaker
use Grape::Middleware::Error, rescue_all: true
run ExceptionSpec::ExceptionApp
end
end
it 'sets the message appropriately' do
get '/'
expect(last_response.body).to eq('rain!')
end
it 'defaults to a 500 status' do
get '/'
expect(last_response.status).to eq(500)
end
end
it 'sets the message appropriately' do
get '/'
expect(last_response.body).to eq('rain!')
end
it 'defaults to a 500 status' do
get '/'
expect(last_response.status).to eq(500)

context 'Non-StandardError exception' do
subject do
Rack::Builder.app do
use Spec::Support::EndpointFaker
use Grape::Middleware::Error, rescue_all: true
run ExceptionSpec::OtherExceptionApp
end
end
it 'does not trap errors other than StandardError' do
expect { get '/' }.to raise_error(NotImplementedError, 'snow!')
end
end
end

Expand Down

0 comments on commit 2833464

Please sign in to comment.