1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00
Files
til/rails/rescue-from.md
2016-03-03 21:26:08 -06:00

688 B

Rescue From

The rescue_from method, provided by ActiveSupport, is a handy way to provide a catch-all response to a particular exception in Rails controllers.

For instance, if many of the controllers in your application raise a User::NotAuthorized error for unauthorized requests, the ApplicationController can provide a unified response. This will help dry up your controllers and prevent any potential inconsistencies.

class ApplicationController < ActionController::Base
  rescue_from User::NotAuthorized do |exception|
    # respond with some Not Authorized page
  end

  ...
end