Created
August 13, 2010 14:11
-
-
Save accuser/522944 to your computer and use it in GitHub Desktop.
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
# app/controllers/application_controller.rb | |
class ApplicationController < ActionController::Base | |
rescue_from(ActionController::RoutingError) { | |
render :template => 'errors/404' | |
} | |
end | |
# config/initializers/show_exceptions.rb | |
require 'action_dispatch/middleware/show_exceptions' | |
module ActionDispatch | |
class ShowExceptions | |
private | |
def render_exception_with_template(env, exception) | |
body = ErrorsController.action(rescue_responses[exception.class.name]).call(env) | |
log_error(exception) | |
body | |
rescue | |
render_exception_without_template(env, exception) | |
end | |
alias_method_chain :render_exception, :template | |
end | |
end | |
# app/controllers/errors_controller.rb | |
class ErrorsController < ApplicationController | |
ERRORS = [ | |
:internal_server_error, | |
:not_found, | |
:unprocessable_entity | |
].freeze | |
ERRORS.each do |e| | |
define_method e do | |
respond_to do |format| | |
format.html { render e, :status => e } | |
format.any { head e } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment