Skip to content

Instantly share code, notes, and snippets.

@satomixx
Created October 8, 2014 04:18
Show Gist options
  • Save satomixx/1460b73aae14bdd35021 to your computer and use it in GitHub Desktop.
Save satomixx/1460b73aae14bdd35021 to your computer and use it in GitHub Desktop.
[Rails 4.x] エラーページの作り方 ref: http://qiita.com/tsumekoara/items/88815b94f63608a801a8
module ErrorHandlers
extend ActiveSupport::Concern
included do
rescue_from Exception, with: :rescue500
rescue_from ApplicationController::Forbidden, with: :rescue403
rescue_from ApplicationController::IpAddressRejected, with: :rescue403
rescue_from ActionController::RoutingError, with: :rescue404
rescue_from ActiveRecord::RecordNotFound, with: :rescue404
end
private
def rescue403(e)
@exception = e
render 'errors/forbidden', status: 403
end
def rescue404(e)
@exception = e
render 'errors/not_found', status: 404
end
def rescue500(e)
@exception = e
render '/errors/internal_server_error', status: 500
end
end
class Forbidden < ActionController::ActionControllerError; end
class IpAddressRejected < ActionController::ActionControllerError; end
include ErrorHandlers if Rails.env.production? or Rails.env.staging?
get '*anything' => 'errors#routing_error'
class ErrorsController < ApplicationController
def routing_error
raise ActionController::RoutingError, "No route matches #{request.path.inspect}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment