Let's make an error screen with Rails

Create an error screen in Rails

Catch the error

The Rails controller has a class method called rescue_from method You can specify how to handle exceptions that occur within an action. The following is 500Error (supplementing the error on the WEB server)

application_controller.rb


#If StandardError occurs, process with rescue500
rescue_from StandardError, with: :rescue500

Process with action

This rescue500 method actually does the processing An argument is specified in the method and an Exception object is included in it.

After render, the path of the ERB template is specified. The render status option is the status code of the HTTP method.

application_controller.rb



private
  #Specify the argument e. Contains an error object
  def rescue500(e)
    render "errors/server_error", status: 500
  end
end

To summarize the above

application_controller.rb


class ApplicationController < ActionController::Base
  rescue_from StandartError, with: :rescue500

  private
  def rescue500(e)
    render "errors/internal_server_error", status: 500
  end
end

It looks like this. Catch an error-> let the action handle it-> display the screen That's the flow.

Add 400 error

Since 500 error is not enough, add 400 error. Add code that catches ActiveRecord :: RecordNotFound errors

application_controller.rb


class ApplicationController < ActionController::Base
  rescue_from StandartError, with: :rescue500
  rescue_from ActiveRecord::RecordNotFound, with: :rescue404

  private

  def rescue400(e)
    render "errors/not_found", status: 404
  end

  def rescue500(e)
    render "errors/internal_server_error", status: 500
  end
end

I was able to branch the process due to an error like this.

By the way

Since the rescue_from method is for catching exceptions raised by actions Errors that occur during the routing process cannot be captured.

So it is necessary to devise ActionController :: RoutingError, but I think that I will call it again next time.

That's all for today. Thank you very much.

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

Recommended Posts

Let's make an error screen with Rails
Let's make a search function with Rails (ransack)
How to make an almost static page with rails
[Rails] Make pagination compatible with Ajax
Let's unit test with [rails] Rspec!
Let's make draw poker with ruby-Implementation 1 (card)-
Let's create an instance with .new yourself. .. ..
Let's make draw poker with ruby ~ Preparation ~
Let's make draw poker with ruby-Implementation 4 (Deck)-
[Rails] Let's manage constants with config gem
Let's make a smart home with Ruby!
Make a login function with Rails anyway
Let's make draw poker with ruby-Implementation 3 (player)-
Make a site template easily with Rails
Let's make draw poker with ruby-Implementation 2 (role)-
rails test fails with database reference error
Downgrade an existing app created with rails 5.2.4 to 5.1.6
[Rails] Let's translate the error message into Japanese
[Rails] Error resolution when generating tokens with PAYJP
Let's make draw poker with ruby ~ test-unit preparation ~
Rails6 I want to make an array of values with a check box
[Ruby on Rails] Let's build an environment on mac
Create an EC site with Rails5 ⑤ ~ Customer model ~
Create an EC site with Rails 5 ⑩ ~ Create an order function ~
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
How to make batch processing with Rails + Heroku configuration
How to push an app developed with Rails to Github
Create an EC site with Rails5 ⑦ ~ Address, Genre model ~
[My memo] Let's get along with Pry / DB with Rails
Display a confirmation screen before registering a record with rails
Let's make a LINE Bot with Ruby + Sinatra --Part 2
Error encountered with notes when deploying docker on rails
Create an EC site with Rails5 ④ ~ Header and footer ~
[Java basics] Let's make a triangle with a for statement
Create an EC site with Rails5 ⑥ ~ seed data input ~
Make Nginx of CentOS8 SSL compatible with Let's Encrypt
Let's make a LINE Bot with Ruby + Sinatra --Part 1
Catch Rails Routing Error
Rails deploy with Docker
[Rails 6] RuntimeError with $ rails s
[Rails error] unexpected tIDENTIFIER
Let's make Rails-like (View)
Mac Rails Install Error
Handle devise with Rails
[Rails] Learning with Rails tutorial
rails heroku error log
rails error resolution summary
[Rails] Test with RSpec
[Rails] What to do if you can't get an error message with the errors method
Let's scrape with Java! !!
Supports multilingualization with Rails!
[Rails] Create an application
Double polymorphic with Rails
Make an Ev3 radio control with JavaFx and leJOS [Part 2]
I want to push an app made with Rails 6 to GitHub
exited with code 1 error resolution with docker-compose up in rails environment
[Rails] Create an email sending function with ActionMailer (complete version)
Summary of initial work when creating an app with Rails
Let's make a simple API with EC2 + RDS + Spring boot ①
[swift5] Try to make an API client with various methods
Display an error screen during the download process to response