Skip to content

Commit 429295f

Browse files
committed
Use default_rescue_handler when reponse is invalid
1 parent 9e945a6 commit 429295f

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#1776](https://github.com/ruby-grape/grape/pull/1776): Validates response processed by exception handler - [@darren987469](https://github.com/darren987469).
1011

1112
### 1.1.0 (8/4/2018)
1213

@@ -21,7 +22,6 @@
2122
* [#1758](https://github.com/ruby-grape/grape/pull/1758): Fix expanding load_path in gemspec - [@2maz](https://github.com/2maz).
2223
* [#1765](https://github.com/ruby-grape/grape/pull/1765): Use 415 when request body is of an unsupported media type - [@jdmurphy](https://github.com/jdmurphy).
2324
* [#1771](https://github.com/ruby-grape/grape/pull/1771): Fix param aliases with 'given' blocks - [@jereynolds](https://github.com/jereynolds).
24-
* [#1776](https://github.com/ruby-grape/grape/pull/1776): Validates response processed by exception handler - [@darren987469](https://github.com/darren987469).
2525

2626
### 1.0.3 (4/23/2018)
2727

lib/grape/middleware/error.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def run_rescue_handler(handler, error)
128128
end
129129

130130
response = handler.arity.zero? ? instance_exec(&handler) : instance_exec(error, &handler)
131-
valid_response?(response) ? response : error!('Internal Server Error(Invalid Response)')
131+
valid_response?(response) ? response : run_rescue_handler(:default_rescue_handler, error)
132132
end
133133

134134
def valid_response?(response)

spec/grape/api_spec.rb

+21-15
Original file line numberDiff line numberDiff line change
@@ -1724,24 +1724,30 @@ class CustomError < Grape::Exceptions::Base; end
17241724
expect(last_response.body).to eq('Formatter Error')
17251725
end
17261726

1727-
it 'validates response processed by exception handler' do
1728-
subject.rescue_from ArgumentError do
1729-
error!('rain!')
1730-
nil # invalid response caused by return nil
1731-
end
1732-
subject.rescue_from :all do
1733-
error!('rain!')
1727+
context 'validates response processed by exception handler' do
1728+
it 'calls default_rescue_handler when response is invalid' do
1729+
subject.rescue_from :all do
1730+
error!('Internal Server Error')
1731+
nil # invalid response caused by return nil
1732+
end
1733+
subject.get('/invalid_response') { raise 'rain!' }
1734+
1735+
expect_any_instance_of(Grape::Middleware::Error).to receive(:default_rescue_handler).and_call_original
1736+
get '/invalid_response'
1737+
expect(last_response.status).to eql 500
1738+
expect(last_response.body).to eq('rain!')
17341739
end
1735-
subject.get('/invalid_response') { raise ArgumentError }
1736-
subject.get('/valid_response') { raise 'rain!' }
17371740

1738-
get '/invalid_response'
1739-
expect(last_response.status).to eql 500
1740-
expect(last_response.body).to eq('Internal Server Error(Invalid Response)')
1741+
it 'calls custom handler when response is valid' do
1742+
subject.rescue_from :all do
1743+
error!('Internal Server Error')
1744+
end
1745+
subject.get('/valid_response') { raise 'rain!' }
17411746

1742-
get '/valid_response'
1743-
expect(last_response.status).to eql 500
1744-
expect(last_response.body).to eq('rain!')
1747+
get '/valid_response'
1748+
expect(last_response.status).to eql 500
1749+
expect(last_response.body).to eq('Internal Server Error')
1750+
end
17451751
end
17461752
end
17471753

0 commit comments

Comments
 (0)