Skip to content

Commit 7b3d8e0

Browse files
committed
Rescue truly all exceptions
1 parent 4782cea commit 7b3d8e0

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [#1752](https://github.com/ruby-grape/grape/pull/1752): Fix `include_missing` behavior for aliased parameters - [@jonasoberschweiber](https://github.com/jonasoberschweiber).
1515
* [#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).
1616
* [#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).
17+
* [#1745](https://github.com/ruby-grape/grape/pull/1745): Rescue truly all exceptions - [@mtsmfm](https://github.com/mtsmfm).
1718
* Your contribution here.
1819

1920
### 1.0.2 (1/10/2018)

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,7 @@ literally accepts every request.
20862086

20872087
## Exception Handling
20882088

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

20912091
```ruby
20922092
class Twitter::API < Grape::API
@@ -2116,7 +2116,7 @@ class Twitter::API < Grape::API
21162116
end
21172117
```
21182118

2119-
In this case ```UserDefinedError``` must be inherited from ```StandardError```.
2119+
```UserDefinedError``` can be inherited from any exception class.
21202120

21212121
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.
21222122

lib/grape/middleware/error.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def rescue_handler_for_grape_exception(klass)
110110
end
111111

112112
def rescue_handler_for_any_class(klass)
113-
return unless klass <= StandardError
113+
return unless klass <= Exception
114114
return unless options[:rescue_all] || options[:rescue_grape_exceptions]
115115

116116
options[:all_rescue_handler] || :default_rescue_handler

spec/grape/middleware/exception_spec.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ def app
103103
run ExceptionSpec::OtherExceptionApp
104104
end
105105
end
106-
it 'does not trap errors other than StandardError' do
107-
expect { get '/' }.to raise_error(NotImplementedError, 'snow!')
106+
it 'sets the message appropriately' do
107+
get '/'
108+
expect(last_response.body).to eq('snow!')
109+
end
110+
it 'defaults to a 500 status' do
111+
get '/'
112+
expect(last_response.status).to eq(500)
108113
end
109114
end
110115
end

0 commit comments

Comments
 (0)