Last active
October 4, 2016 12:06
-
-
Save axtutuu/fd0559f9350ce3c1c030a9254f907550 to your computer and use it in GitHub Desktop.
Ruby on 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
class ErrorsController < ActionController::Base | |
NOT_FOUND = 404 | |
STANDARD_ERROR = 500 | |
rescue_from StandardError, with: :error_500 | |
rescue_from AbstractController::ActionNotFound, with: :render_404 | |
rescue_from ActionController::RoutingError, with: :render_404 | |
layout false | |
def render_404(e) | |
respond_to do |format| | |
format.html { render file: "#{Rails.root}/public/404.html", status: NOT_FOUND } | |
format.json { render json: { status: NOT_FOUND, result: e.try(:message) } } | |
end | |
end | |
def render_500(e) | |
logger.error "500 exception: #{e.try(:message)}" | |
respond_to do |format| | |
format.html { render file: "#{Rails.root}/public/500.html", status: STANDARD_ERROR } | |
format.json { render json: { status: STANDARD_ERROR, result: e.try(:message) } } | |
end | |
end | |
def thow; raise request.env["action_dispatch.exception"]; end | |
end |
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
Rails.application.configure do | |
config.exceptions_app = ->(env) { ErrorsController.action(:thow).call(env) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment