DEV Community 👩‍💻👨‍💻

n350071🇯🇵
n350071🇯🇵

Posted on

Return a static error page by Rails standard function

👍 Use config.application.rb

config.action_dispatch.rescue_responses.merge!(
     ‘My::Error’ => :not_found
   )

📚 Reference

3.9 Configuring Action Dispatch

config.action_dispatch.rescue_responses configures what exceptions are assigned to an HTTP status. It accepts a hash and you can specify pairs of exception/status. By default, this is defined as:

Any exceptions that are not configured will be mapped to 500 Internal Server Error.

config.action_dispatch.rescue_responses = {
  'ActionController::RoutingError'               => :not_found,
  'AbstractController::ActionNotFound'           => :not_found,
  'ActionController::MethodNotAllowed'           => :method_not_allowed,
  'ActionController::UnknownHttpMethod'          => :method_not_allowed,
  'ActionController::NotImplemented'             => :not_implemented,
  'ActionController::UnknownFormat'              => :not_acceptable,
  'ActionController::InvalidAuthenticityToken'   => :unprocessable_entity,
  'ActionController::InvalidCrossOriginRequest'  => :unprocessable_entity,
  'ActionDispatch::Http::Parameters::ParseError' => :bad_request,
  'ActionController::BadRequest'                 => :bad_request,
  'ActionController::ParameterMissing'           => :bad_request,
  'Rack::QueryParser::ParameterTypeError'        => :bad_request,
  'Rack::QueryParser::InvalidParameterError'     => :bad_request,
  'ActiveRecord::RecordNotFound'                 => :not_found,
  'ActiveRecord::StaleObjectError'               => :conflict,
  'ActiveRecord::RecordInvalid'                  => :unprocessable_entity,
  'ActiveRecord::RecordNotSaved'                 => :unprocessable_entity
}

🔗 Parent Note

Top comments (0)

👋 New to DEV?

Head over to our Welcome Thread and tell us a bit about yourself!