in PHP

Introducing a new error handler for PHP projects

Dealing with errors is one of the most frustrating and challenging parts of developing an application. Nobody likes to think about their application in a failure state, and there’s nothing more deflating than writing a bunch of great code, and getting a message that you forgot a semicolon on line 4.

After trying a few packages and becoming frustrated with the nature of the available packages, I decided to write one. I called it BooBoo, because everyone makes mistakes. BooBoo is a package for error handling in PHP.

The way an error handler should work

The existing solutions typically treat every error, from a notice to an exception, as fatal and kill the application entirely. I felt that was a mistake; if PHP had wanted notices to be fatal, they would have made notices fatal. Even though I write my code in such a way to avoid notices and warnings, if one happens in production that doesn’t actually affect the user’s behavior, I don’t feel that my code should completely break.

BooBoo is designed with this in mind. Errors that are fatal get treated as fatal. Errors that aren’t fatal allow the script to continue running. For those that like to break their app on every small error, there’s an option for enabling that behavior, though I always run with that feature turned off.

In addition, I want to perform operations on my errors in production, but I only want to display them in development. BooBoo is designed with two concepts: the Handler, and the Formatter.

The Handler actually performs an operation on the error, like logging or submission to Sentry. The Formatter, on the other hand, turns the error into something readable for the screen and SAPI you’re using. BooBoo ships with a PSR-3 compliant logging handler. It also ships with several formatters for displaying errors as tables, on the CLI, and as JSON (for REST API development). When display_errors is turned off, BooBoo will not run the formatters (but the handlers will run); in addition, BooBoo lets you specify a formatter for handling fatal errors with a pretty error page.

A proud part of The League of Extraordinary Packages

The concept behind BooBoo is bigger than just me as a developer. It should grow and change along with PHP. With conversations about engine exceptions and new error types happening for PHP 7 and beyond, maintenance and good quality standards become crucial. That’s why I submitted the package to The League of Extraordinary Packages: there, it will get the support and care it needs, even if someday I’m not able to maintain it.

The League of Extraordinary Packages is dedicated to great code, and nothing else. BooBoo is a part of the League; I am just the project lead. I like that, because I want the package to be bigger than any one person.

Try BooBoo today!

BooBoo is in 1.0.0 release, and is available for your next project. Check it out, and as always, pull requests welcome!

Frustrated with your company’s development practices?

You don't have to be!

No matter what the issues are, they can be fixed. You can begin to shed light on these issues with my handy checklist.

Plus, I'll help you with strategies to approach the issues at the organization level and "punch above your weight."

Powered by ConvertKit
Brandon Savage is the author of Mastering Object Oriented PHP and Practical Design Patterns in PHP