Posts tagged with error handling

Adding try/catch to Laravel collections

by Freek Van der Herten – 6 minute read

A few weeks ago, Jmac tweeted out an excellent idea. What if we could use try and catch in a collection chain?

Meanwhile, Jmac and I did a few code pairing sessions to work on a possible implementation. We've added try and catch methods to the laravel-collection-macros package.

In this blog post, I'd like to share what you can do with these methods and how they work under the hood.

Read more

Join thousands of developers

Every two weeks, I share practical tips, tutorials, and behind-the-scenes insights from maintaining 300+ open source packages.

No spam. Unsubscribe anytime. You can also follow me on X.

Laravel and Murphy’s Law

medium.com

Patrick Brouwers, the creator of Laravel Excel, explains how to handle failing jobs in Laravel

When designing software, don’t only think about the happy path. Write down (preferably with (unit) tests) what all the things are that could go wrong. Then design your solution to be able to recover those situations. (Wether or not automatic.) There isn’t a single solution to rule them all, some processes might need to have specific failure handling while others are fine with the default approach.

Read more [medium.com]

Exceptional Exceptions

engagor.github.io

At the Clarabridge Developers blog, Toon Daelman wrote a good post on how to improve your exceptions.

You've made it to this post thinking "Why do we still need to talk about Exceptions?". Well, they're used everywhere in OOP codebases, but sometimes they're used in a way that make debugging a bit difficult. Let's look at some ways to make debugging exceptions a bit more fun!

Read more [engagor.github.io]

Honeybadger for Laravel Nova

blog.honeybadger.io

Marcel Pociot recently created a Nova tool for Honeybadger. On their blog Marcel gives some interesting details on how it was created.

In the last weeks, I've been working with the team from Honeybadger on a custom resource tool to add Honeybadger error tracking output to Laravel Nova. It's a great addition to Nova and allows the developer to easily get access to error tracking information that, for example, is associated with specific users.

Read more [blog.honeybadger.io]

The art of the error message

thestyleofelements.org

Marina Posniak, UX writer at Spotify, shares some great tips on how to write error messages well.

To start, ask yourself if you even need the error message. Before writing anything, consider if there’s a way to redesign the experience so there’s no error at all. Is there a way to just make it work? (Really, the best error message is no error message.) But if you do need it, think carefully about the message. When things go wrong and the app “fails,” say something useful. The message should help the user solve the problem and move on.

Read more [thestyleofelements.org]

Debugging the dreaded "Class log does not exist" error in Laravel

My colleague Sebastian took the time to write down the solution to a problem many artisans will come across at some point in time. I hope that in a future version of Laravel that error message will be improved so that it makes clear what the actual problem really is.

Every now and then I come across a Class log does not exist exception in Laravel. This particular exception is thrown when something goes wrong really early in the application, before the exception handler is instantiated.

Whenever I come across this issue I'm stumped. Mostly it's related to an invalid configuration issue or an early service provider that throws an exception. I always forget how to debug this, so it's time to document my solution for tracking down the underlying error.

https://sebastiandedeyne.com/posts/2017/debugging-the-dreaded-class-log-does-not-exist-error-in-laravel

Read more

Use sane defaults over exceptions

Freek Lijten, a developer at Schiphol, makes the case for just setting a sane default value instead of throwing an exception when invalid input entered the application.

I didn't think much of this, but I've seen a major drawback lately while working on a site that is a bit bigger than I was used to. With over half a million visitors a week and lots of scrapers, bots and other stuff visiting, these exceptions and fatal errors clog up logging quite a bit. Not to the point that we can't handle the volume, but it generates false positives in monitoring channels and it is something we do not want to act upon anyway.

http://www.freeklijten.nl/2017/01/04/Sane-defaults-over-Exceptions

Read more

Structuring PHP exceptions

Alain Schlesser wrote an article on how to manage exceptions in a large codebase.

I seem to constantly work on improving my habits regarding the use of exceptions. I think it is an area that I haven’t yet fully explored, and it is very difficult to find anything more than very basic explanations and tutorials online. While the consensus is to use exceptions, there is very little information on how to structure and manage them in a larger codebase. The larger and more complex your projects become, the more important it is to start with a proper structure to avoid expensive refactoring later on.

https://www.alainschlesser.com/structuring-php-exceptions/

In my opinion a good exception message in most cases contains three things:

  • the reason why something went wrong
  • the data that caused the problem
  • suggestions on how to solve the problem

Named constructors for exceptions are the perfect place to build up such a message. Want to learn more? Ross Tuck wrote a good blog post on the subject too.

Read more

Finally, file streams, and deferred execution in PHP

Frank de Jonge wrote an article on how to structure to your functions to avoid the duplication of cleanup code. Spoiler: use finally.

Cleaning up after yourself can be a tedious task. For example, closing file handlers after using them needs to be done. A programmer's life isn't all about the happy path. When things go pear-shaped you might end up duplicating cleanup code throughout your code. This is horrible, let's explore an alternative.

https://blog.frankdejonge.nl/finally-file-streams-and-deferred-execution-in-php/

That defer keyword from Go looks super nice.

Read more