https://github.com/tejasbubane/api_rescuable
Exception Handler for Rails APIs
https://github.com/tejasbubane/api_rescuable
exceptions rails rails-api
Last synced: about 1 month ago
JSON representation
Exception Handler for Rails APIs
- Host: GitHub
- URL: https://github.com/tejasbubane/api_rescuable
- Owner: tejasbubane
- Created: 2017-03-02T18:39:28.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-03T10:37:55.000Z (over 9 years ago)
- Last Synced: 2025-09-02T02:47:08.439Z (10 months ago)
- Topics: exceptions, rails, rails-api
- Language: Ruby
- Size: 12.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# API Rescuable
[](https://travis-ci.org/tejasbubane/api_rescuable)
Quick and easy way to handle common exceptions in Rails APIs.
With Rails APIs you generally want to handle the most common exceptions and give proper errors to your users.
This gem gives you a one-liner to put in your top-level API controller.
#### Installation:
Add the gem to your gemfile:
```ruby
gem "api_rescuable"
```
And run `bundle install`.
Then add this to your top-level API controller:
```ruby
class ApiController < ApplicationController
include ApiRescuable
rescue_from_api ActiveRecord::RecordNotFound,
ActiveRecord::RecordInvalid,
ActiveModel::ForbiddenAttributesError,
ActionController::ParameterMissing,
CanCan::AccessDenied
.....
end
```
For API-only apps this will be `ApplicationController`, for others create a
separate `ApiController` and let all APIs-related controllers inherit from this.
Handles these five exceptions gracefully for you:
* `ActiveRecord::RecordNotFound`
* `ActiveRecord::RecordInvalid`
* `ActiveModel::ForbiddenAttributesError`
* `ActionController::ParameterMissing`
* `CanCan::AccessDenied`
It essentially turns [this](https://github.com/tejasbubane/api_rescuable/blob/master/example_controller.rb)
into a one-liner.
* This gem adds [rescue_from](http://apidock.com/rails/ActiveSupport/Rescuable/ClassMethods/rescue_from)
to the class that includes `ApiRescuable`.
Hence it is supposed to be used in Rails controllers only.
* In case you want finer control over the status codes and/or json being sent,
please use the [example](https://github.com/tejasbubane/api_rescuable/blob/master/example_controller.rb)
and add your own handlers.
* Keep in mind that this gem will add `handle_*` methods to your controller
corresponding to the exceptions being handled.