Skip to content

Commit

Permalink
Update README to use error!.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny Juneja committed Apr 1, 2015
1 parent 2038df2 commit 9cca39c
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ You can rescue a `Grape::Exceptions::ValidationErrors` and respond with a custom
```ruby
format :json
subject.rescue_from Grape::Exceptions::ValidationErrors do |e|
rack_response e.to_json, 400
error!(e, 400)
end
```

Expand Down Expand Up @@ -1442,17 +1442,29 @@ class Twitter::API < Grape::API
end
```

You can rescue all exceptions with a code block. The `error_response` wrapper
You can rescue all exceptions with a code block. The `error!` wrapper
automatically sets the default error code and content-type.

```ruby
class Twitter::API < Grape::API
rescue_from :all do |e|
error_response({ message: "rescued from #{e.class.name}" })
error!("rescued from #{e.class.name}")
end
end
```

Optionally, you can set the format, status code and headers.

```ruby
class Twitter::API < Grape::API
format :json
rescue_from :all do |e|
error!({ error: "Server error.", 500, { 'Content-Type' => 'text/error' } })
end
end
```


You can also rescue specific exceptions with a code block and handle the Rack
response at the lowest level.

Expand All @@ -1469,10 +1481,11 @@ Or rescue specific exceptions.
```ruby
class Twitter::API < Grape::API
rescue_from ArgumentError do |e|
Rack::Response.new([ "ArgumentError: #{e.message}" ], 500).finish
error!("ArgumentError: #{e.message}")
end

rescue_from NotImplementedError do |e|
Rack::Response.new([ "NotImplementedError: #{e.message}" ], 500).finish
error!("NotImplementedError: #{e.message}")
end
end
```
Expand All @@ -1492,10 +1505,10 @@ Then the following `rescue_from` clause will rescue exceptions of type `APIError

```ruby
rescue_from APIErrors::ParentError do |e|
Rack::Response.new({
error!({
error: "#{e.class} error",
message: e.message
}.to_json, e.status).finish
}, e.status)
end
```

Expand All @@ -1504,11 +1517,11 @@ The code below will rescue exceptions of type `RuntimeError` but _not_ its subcl

```ruby
rescue_from RuntimeError, rescue_subclasses: false do |e|
Rack::Response.new({
error!({
status: e.status,
message: e.message,
errors: e.errors
}.to_json, e.status).finish
}, e.status)
end
```

Expand Down

0 comments on commit 9cca39c

Please sign in to comment.