Skip to content

Instantly share code, notes, and snippets.

@gabceb
Created Mar 8, 2013
Embed
What would you like to do?
Handling 404, 401 and 500 exceptions on Rails
class ApplicationController < ActionController::Base
unless Rails.application.config.consider_all_requests_local
#rescue_from Exception, with: :render_500
rescue_from ActionController::RoutingError, with: :render_canvas_404
rescue_from ActionController::UnknownController, with: :render_404
rescue_from AbstractController::ActionNotFound, with: :render_404
rescue_from ActiveRecord::RecordNotFound, with: :render_404
rescue_from ActiveResource::ForbiddenAccess, with: :render_403
end
def render_404(exception)
backtrace = exception.respond_to?(:backtrace) ? exception.backtrace : String.new
logger.warn "**** NOT FOUND backtrace: #{exception.backtrace.slice(0,10).join("\n")} ******"
@gallery_link = true
respond_to do |format|
format.html { render 'errors/error_404', :layout => "application", :status => 404 }
format.xml { head 404 }
format.any { head 404 }
end
end
def render_canvas_404(exception)
@not_found_path = exception.message
@gallery_link = true
respond_to do |format|
format.html { render 'errors/error_404', :layout => "application", :status => 404 }
format.xml { head 404 }
format.any { head 404 }
end
end
def render_500(exception)
@error = exception
respond_to do |format|
format.html { render 'errors/error_500', :layout => "application", :status => 500 }
format.xml { head 500 }
format.any { head 500 }
end
end
def render_403(exception)
respond_to do |format|
format.html { render 'errors/error_403.html.erb', :layout => "application", :status => 403 }
format.xml { head 403 }
format.any { head 403 }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment