From 3f95eb26e416b9b726457273e23886a9ba11be1f Mon Sep 17 00:00:00 2001 From: darren987469 Date: Thu, 9 Aug 2018 23:57:10 +0800 Subject: [PATCH] Add fail spec for #1757 --- lib/grape/middleware/error.rb | 8 +------- spec/grape/api_spec.rb | 30 ++++++++---------------------- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/lib/grape/middleware/error.rb b/lib/grape/middleware/error.rb index 469c350d84..0991b48842 100644 --- a/lib/grape/middleware/error.rb +++ b/lib/grape/middleware/error.rb @@ -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 diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index d1fae42654..44481ab9ba 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -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