Catch Rails Routing Error

Catch RoutingError

This article is a continuation of the following article Let's make an error screen with Rails

The class method rescue_from is for catching exceptions that occur in an action, so it cannot catch the processing that occurs at the routing stage.

So you need to create a file in the config / initializers directory.

The action to be called is sorted according to the value of request.path_info.

404 => not_found action 422 => unprocessable_entity Other => internal_server_error

After that, action is passed as an argument to ErrorsController and called.

exceptions_app.rb


Rails.application.configure do
  config.exceptions_app = ->(env) do
    request = ActionDispatch::Request.new(env)
    
    action =
      case request.path_info
      when "/404"; :not_found
      when "/422"; :unprocessable_entity
      else; :internal_server_error
      end
    ErrorsController.action(action).call(env)
  end
end

The controller looks like this: The view to be displayed by render is specified for each action.

errors_controller.rb


class ErrorsController < ApplicationController
  def not_found
    render status: 404
  end
  
  def unprocessable_entity
    render status: 422
  end

  def internal_server_error
    render status: 500
  end
end

So there are also routing errors that cannot be caught by rescue_from I was able to handle the error and display the screen as described above.

That's all for today.

** 80 days to become a full-fledged engineer **

Recommended Posts

Catch Rails Routing Error
About Rails routing
Rails Routing Basics
Rails 6.0 Routing Summary
[Note] Rails error list
[Rails] devise-related routing summary
[Rails error] unexpected tIDENTIFIER
Mac Rails Install Error
rails heroku error log
rails error resolution summary
[Note] Rails3 routing confirmation
Understand Rails "shallow" routing
[Rails] Complete routing settings
rails error Library not loaded
Error in rails db: migrate
Organize Rails routing using draw
[Rails] Rails version upgrade error memorandum
Rails routing controller view relationship
Error when using rails capybara
How to write Rails routing
[rails] error during devise installation
Rails singular resource routing by resource
[Rails] What was the error message?
[Rails] Japanese localization of error messages
[Rails] Unexpected validation error in devise
[Rails] Summary of complicated routing configurations
Set Rails routing other than id
[Rails] Display form error messages asynchronously
Japanese localization of error messages (rails)
Ruby On Rails devise routing conflict
routing
[Rails] syntax error, unexpected tSTRING_END, expecting'''
[Rails] AWS deployment error encounter summary
Ruby on Rails [Error memorandum] Routing Error No route matches [DELETE] "/ users / sign_out"
[Ruby on Rails] 1 model CRUD (Routing Main)
Rails / users /: id / to / {random_srting}: Dedefault Routing
[Rails] How to display error messages individually
Testing for Error Messages: Rails Tutorial Notes-Chapter 7
[Ruby / Rails] Mechanism for retrying Thread Error
[rails] How to configure routing in resources
[rails s error] md5.bundle and mysql installation error
rails test fails with database reference error
Rails console Incorrect string value error handling
Let's make an error screen with Rails
[Rails / Heroku] Error resolution procedure after push