0.0
No commit activity in last 3 years
No release in over 3 years
Quick and easy way to handle common exceptions in Rails APIs
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

= 5.0.2
 Project Readme

API Rescuable

Build Status

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:

gem "api_rescuable"

And run bundle install.

Then add this to your top-level API controller:

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 into a one-liner.

  • This gem adds 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 and add your own handlers.

  • Keep in mind that this gem will add handle_* methods to your controller corresponding to the exceptions being handled.