When everything fails, you can always trust the Laravel rescue helper
Your application probably has a number of minor and more significant bugs. Some bugs will destroy your application, making it impossible to use, while others can be ignored.
The problem? When something goes wrong, we love to throw an exception; it is so easy:
throw new Exception("This thing went wrong, I'm done here it is up to the next person to handle this")
But that exception doesn't tell you anything about the severity of what happened. It could be a TypeError where you used a string for a function argument while a complex object with multiple data properties is required. It could also be that you're sending an email notification to someone whose email address no longer exists, so the request failed.
In that first case, your application breaks, and that's probably best; we're trying to do something in our application with data that is not valid for the operation. While the second example shouldn't break the application, we'll want to remove that email address from our lists so that we stop sending to it next time.
Try/Catch to the ... rescue
Luckily, this problem can be quickly solved by using a try/catch statement:
try {
sendNotification($email, $contents);
} catch (EmailDoesNotExist) {
removeEmail($email);
}
Now, what if we want to track these EmailDoesNotExist
exceptions? As said earlier, code always has bugs, and it could be that something is broken in this piece of code, and the email address is not being removed.
It would be handy to send these exceptions to an error tracker like Flare, which is the best error tracker for Laravel!
Laravel error handling to the ... rescue
We can change our try-catch statement like this:
try {
sendNotification($email, $contents);
} catch (EmailDoesNotExist $exception) {
report($exception);
removeEmail($email);
}
The exception will be reported through Laravel's error handling system and eventually sent to a chosen error tracker, but your application will keep running like nothing happened.
A cool feature of Flare is that we mark these kinds of exceptions as handled with an extra label:
This label makes it easy to see if you should start panicking if an error is happening a bazillion times or if it is just business as usual.
But ... we're working with Laravel. Can we rewrite these lines of code to some of that polished beautiful Laravel code?
Laravel rescue to the ...
Laravel provides a rescue helper that mimics the try/catch statement we've written earlier. It works like this:
rescue(
fn() => sendNotification($email, $contents),
fn() => removeEmail($email)
);
You've got to love the beauty of those four lines of code!
Do you want to decide if you're going to report the exception? You can add a third closure like this:
rescue(
fn() => sendNotification($email, $contents),
fn() => removeEmail($email),
fn(EmailDoesNotExist $exception) => str_contains($exception->getMessage(), "@spatie.be")
);
Conclusion
Laravel has many of these tiny helpers. The rescue helper is probably one of my favorites. They allow you to write more readable code even faster!