rescue_from with Ruby on Rails

Gokul
4 min readJan 19, 2023

What?

The Ruby on Rails framework has a function known as rescue_from that gives you the ability to manage errors in a centralised and well-organized fashion. It is used to rescue specific exception classes or modules and handle them in a specific way.

When an error occurs in the code, the interpreter will search for an appropriate rescue block to use in order to deal with the error. rescue_from is a method that gives you the ability to choose which exceptions should be rescued and what actions should be carried out after they have been rescued. For instance, you might use it to send the visitor to a new page, display an error message to them, or report the issue they encountered.

It can be used in controllers, services, or any other class where you want to handle exceptions. When an exception is rescued, the interpreter will jump to the matching rescue block, execute the code inside it, and then continue executing the rest of the program.

Why?

rescue_from in Ruby on Rails is a powerful tool that allows you to handle exceptions in a more organized and consistent way, improving the overall quality and maintainability of your code and providing a better user experience.

Centralized error handling

--

--

Gokul

Consultant | Freelancer | Ruby on Rails | ReactJS