Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rescue truly all exceptions #1745

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [#1752](https://github.com/ruby-grape/grape/pull/1752): Fix `include_missing` behavior for aliased parameters - [@jonasoberschweiber](https://github.com/jonasoberschweiber).
* [#1754](https://github.com/ruby-grape/grape/pull/1754): Allow rescue from non-`StandardError` exceptions to use default error handling - [@jelkster](https://github.com/jelkster).
* [#1756](https://github.com/ruby-grape/grape/pull/1756): Allow custom Grape exception handlers when the built-in exception handling is enabled - [@soylent](https://github.com/soylent).
* [#1745](https://github.com/ruby-grape/grape/pull/1745): Rescue truly all exceptions - [@mtsmfm](https://github.com/mtsmfm).
* Your contribution here.

### 1.0.2 (1/10/2018)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2086,7 +2086,7 @@ literally accepts every request.

## Exception Handling

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

```ruby
class Twitter::API < Grape::API
Expand Down Expand Up @@ -2116,7 +2116,7 @@ class Twitter::API < Grape::API
end
```

In this case ```UserDefinedError``` must be inherited from ```StandardError```.
```UserDefinedError``` can be inherited from any exception class.

Notice that you could combine these two approaches (rescuing custom errors takes precedence). For example, it's useful for handling all exceptions except Grape validation errors.

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def rescue_handler_for_grape_exception(klass)
end

def rescue_handler_for_any_class(klass)
return unless klass <= StandardError
return unless klass <= Exception
return unless options[:rescue_all] || options[:rescue_grape_exceptions]

options[:all_rescue_handler] || :default_rescue_handler
Expand Down
9 changes: 7 additions & 2 deletions spec/grape/middleware/exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ def app
run ExceptionSpec::OtherExceptionApp
end
end
it 'does not trap errors other than StandardError' do
expect { get '/' }.to raise_error(NotImplementedError, 'snow!')
it 'sets the message appropriately' do
get '/'
expect(last_response.body).to eq('snow!')
end
it 'defaults to a 500 status' do
get '/'
expect(last_response.status).to eq(500)
end
end
end
Expand Down