Skip to content

Commit

Permalink
Add fail spec for ruby-grape#1757
Browse files Browse the repository at this point in the history
  • Loading branch information
darren987469 committed Aug 9, 2018
1 parent 429295f commit 3f95eb2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 29 deletions.
8 changes: 1 addition & 7 deletions lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,7 @@ def run_rescue_handler(handler, error)
handler = public_method(handler)
end

response = handler.arity.zero? ? instance_exec(&handler) : instance_exec(error, &handler)
valid_response?(response) ? response : run_rescue_handler(:default_rescue_handler, error)
end

def valid_response?(response)
# Rack::Response.new(...).finish generates an array with size 3
response.is_a?(Array) && response.size == 3
handler.arity.zero? ? instance_exec(&handler) : instance_exec(error, &handler)
end
end
end
Expand Down
30 changes: 8 additions & 22 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1724,30 +1724,16 @@ class CustomError < Grape::Exceptions::Base; end
expect(last_response.body).to eq('Formatter Error')
end

context 'validates response processed by exception handler' do
it 'calls default_rescue_handler when response is invalid' do
subject.rescue_from :all do
error!('Internal Server Error')
nil # invalid response caused by return nil
end
subject.get('/invalid_response') { raise 'rain!' }

expect_any_instance_of(Grape::Middleware::Error).to receive(:default_rescue_handler).and_call_original
get '/invalid_response'
expect(last_response.status).to eql 500
expect(last_response.body).to eq('rain!')
it 'can rescue exception no mater what returned by rescue_from' do
subject.rescue_from :all do
error!('Internal Server Error')
Grape::API.logger.error('Internal Server Error')
end
subject.get('/') { raise 'rain!' }

it 'calls custom handler when response is valid' do
subject.rescue_from :all do
error!('Internal Server Error')
end
subject.get('/valid_response') { raise 'rain!' }

get '/valid_response'
expect(last_response.status).to eql 500
expect(last_response.body).to eq('Internal Server Error')
end
get '/'
expect(last_response.status).to eql 500
expect(last_response.body).to eq 'Internal Server Error'
end
end

Expand Down

0 comments on commit 3f95eb2

Please sign in to comment.