Last active
August 29, 2015 14:01
-
-
Save yuki24/09f373a318a4ac56f5a9 to your computer and use it in GitHub Desktop.
Error handler that generates error pages dynamically in Rails
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
# config/application.rb | |
config.exceptions_app = ->(env) { ErrorsController.action(:show).call(env) } |
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/errors_controller.rb | |
class ErrorsController < ActionController::Base | |
layout 'application' | |
rescue_from StandardError, with: :internal_server_error | |
rescue_from ActiveRecord::RecordNotFound, ActionController::RoutingError, with: :not_found | |
def not_found(exception = nil) | |
render template: "errors/not_found", status: 404 | |
end | |
def internal_server_error(exception = nil) | |
render template: "errors/internal_server_error", status: 500 | |
end | |
def show | |
raise env["action_dispatch.exception"] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment