Created
October 15, 2012 10:38
-
-
Save ajbonner/3891892 to your computer and use it in GitHub Desktop.
How not to rescue_from exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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