The art of handling errors with rescue_from and Rails
Tags: ruby, rails, webdev, programming
Published: 2022-10-11 13:07:59

In the Ruby programming language, exceptions are a common feature. About 30 different exception subclasses are defined in the Ruby standard library, some of which have their subclasses. 

Let's go over how to use rescue exceptions in ruby and how to handle them.

**Errors Are Actually Good**
A quick preface before we go any deeper: errors should be embraced during the development stage of your code. While you certainly don’t want a user to hit an error page, embracing errors as a normal part of software development is a healthy mindset to get into.

**What is an Exception?**
An exception represents an error condition in a program. Exceptions are a mechanism for stopping a program's execution. Unhandled exceptions cause Ruby programs to crash.

**What is Exception Handling?**
Software systems are typically prone to errors. Exceptions are more likely to occur in systems that include human involvement because they attract errors on numerous levels.

Syntactical errors, network errors, form input errors, invalid authentication errors, and so on are all examples of errors. These can damage user experience and potentially emerge as security loopholes, allowing attackers to compromise the system if not addressed.

_Exception handling prevents system failures and saves the user from annoying error messages._

When an error condition occurs, the programming language usually compels the program to terminate, and control of the program is lost. Exception handling allows you to maintain control and respond to what occurs after an error has occurred.

_This offers you some flexibility to make changes, such as displaying friendly error messages, releasing resources, or quickly redirecting the user to a relevant website._

**rescue_from and Rails**
Starting from the release 2.0, Rails provides a clean way to rescue exceptions in a controller, mapping specific error classes to corresponding handlers.

**rescue_from** receives a series of exception classes or class names, and a trailing :with option with the name of a method or a Proc object to be called to handle them. Alternatively a block can be given.

_Let's see an example._ A call to ActiveRecord#find raises an ActiveRecord::RecordNotFound exception when the record passed as parameter doesn't exist. Assuming you want to display a nice error message, you need to rescue the exception in each action where a find call is performed.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1zxjeew9nqmvrsmuzm5v.png)


This approach leads to lot of code duplication if you count the number of find calls for each action per model. 

The **rescue_from method** is exactly the **solution** we are looking for. Instead of catching the exception at action-level, we instruct the controller to rescue all the ActiveRecord::RecordNotFound errors and forward the exception to the proper handler.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4exh3ftx09mgcpcp2kc.png)

Voilà! Now you have much cleaner code and displaying friendly error messages :)

P.S. Example of React side finished error handling product

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dda6gq2htn2vjbk1aoni.png)