Skip to content

Commit

Permalink
Merge pull request #1725 from Jelkster/rescue-all-doc-fix
Browse files Browse the repository at this point in the history
`rescue_from :all` documentation fix
  • Loading branch information
dblock authored Jan 1, 2018
2 parents 8395a41 + 69e2109 commit fda88b5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [#1710](https://github.com/ruby-grape/grape/pull/1710): Fix wrong transformation of empty Array in declared params - [@pablonahuelgomez](https://github.com/pablonahuelgomez).
* [#1722](https://github.com/ruby-grape/grape/pull/1722): Fix catch-all hiding multiple versions of an endpoint after the first definition - [@zherr](https://github.com/zherr).
* [#1724](https://github.com/ruby-grape/grape/pull/1724): Optional nested array validation - [@ericproulx](https://github.com/ericproulx).
* [#1725](https://github.com/ruby-grape/grape/pull/1725): Fix `rescue_from :all` documentation - [@Jelkster](https://github.com/Jelkster).
* Your contribution here.

### 1.0.1 (9/8/2017)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2071,7 +2071,7 @@ literally accepts every request.

## Exception Handling

Grape can be told to rescue all exceptions and return them in the API format.
Grape can be told to rescue all `StandardError` exceptions and return them in the API format.

```ruby
class Twitter::API < Grape::API
Expand Down Expand Up @@ -2177,8 +2177,8 @@ class Twitter::API < Grape::API
error!("ArgumentError: #{e.message}")
end

rescue_from NotImplementedError do |e|
error!("NotImplementedError: #{e.message}")
rescue_from NoMethodError do |e|
error!("NoMethodError: #{e.message}")
end
end
```
Expand Down
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 fda88b5

Please sign in to comment.