Skip to content

Commit add9989

Browse files
author
Darren
committed
Add rescue_from description in README
1 parent dfffd7f commit add9989

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

README.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -2183,7 +2183,7 @@ You can also rescue all exceptions with a code block and handle the Rack respons
21832183
```ruby
21842184
class Twitter::API < Grape::API
21852185
rescue_from :all do |e|
2186-
Rack::Response.new([ e.message ], 500, { 'Content-type' => 'text/error' }).finish
2186+
Rack::Response.new([ e.message ], 500, { 'Content-type' => 'text/error' })
21872187
end
21882188
end
21892189
```
@@ -2254,7 +2254,16 @@ class Twitter::API < Grape::API
22542254
end
22552255
```
22562256

2257-
The `rescue_from` block must return a `Rack::Response` object, call `error!` or re-raise an exception.
2257+
By default, object returned by `rescue_from` would be taken as response message. It is suggested to call `error!` in the last line of `rescue_from` block. But you could return `String`, `Hash`, `Array`, `Rack::Response` or even `nil` as well.
2258+
2259+
```ruby
2260+
rescue_from(:all) { error!('error') }
2261+
rescue_from(:all) { 'error' }
2262+
rescue_from(:all) { { message: 'error' } }
2263+
rescue_from(:all) { [500, 'error', { 'Content-Type' => 'text/error' }] }
2264+
rescue_from(:all) { Rack::Response.new('error', 500) }
2265+
rescue_from(:all) { nil } # response default_message and default_status
2266+
```
22582267

22592268
The `with` keyword is available as `rescue_from` options, it can be passed method name or Proc object.
22602269

0 commit comments

Comments
 (0)