Insights
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!
Continue reading
Lessons from the deep end
20 months ago, we started building Performance Monitoring as Flare’s next big feature, never expecting Laravel’s rapid commercial growth to put us in direct competition with their own tools. This is our honest take on those 20 months went, how we’re adapting to this new reality, and where we’re heading next while staying true to who we are. A dive into the deep end, without knowing how far down it goes.
Alex
Connect your AI agent to Flare to automatically fix production and performance problems in PHP and Laravel projects
You can now use our MCP server to connect your AI agent to Flare. This way your AI has all context it needs to diagnose and fix production and performance problems.
Freek
Subscribe to Backtrace, our quarterly Flare newsletter
No spam, just news & product updates