Skip to content

Instantly share code, notes, and snippets.

@ajbonner
Created October 15, 2012 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ajbonner/3891892 to your computer and use it in GitHub Desktop.
Save ajbonner/3891892 to your computer and use it in GitHub Desktop.
How not to rescue_from exceptions
class ApplicationController < ActionController::Base
# Catch all exceptions at a stretch
rescue_from Exception, :with => :handle_exceptions
private
# Handle exceptions
def handle_exceptions(e)
case e
# Handling exception of CanCan gem
when CanCan::AccessDenied
authenticate_user!
# Handling exception when record not found
when ActiveRecord::RecordNotFound
not_found
# Or handle any other exceptions
else
internal_error(e)
end
end
def not_found
# Render 404 error page
render 'application/not_found', :status => 404
end
def internal_error(exception)
if RAILS_ENV == 'production'
# Here you can send e-mail to developer or notify Hoptoad
# ...
# Render a pretty page for production with an error message
render :layout => 'layouts/internal_error',
:template => 'application/internal_error',
:status => 500
else
# Forward exception that we were able to see a standard error page with stack for development.
# With no arguments, it re-raises the recent exception.
raise
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment