It’s time for a custom error handler. The PHP core error handler is ok, but we want more.

We will implement two error handlers.

  1. For production
  2. For development

Of course you can use the production error handler in your development environment, but you will have less information 😉

You never should use the development error handler in production, because you will output to many information about your server to the user who see the error message.

To have a proper error handler we also need some mail functionality. For this we will use the PhpMailer library.

If an error occurs you should be notified immediately.

When an error occurs in my projects, I will be notified in HipChat, per mail and store the error in a logfile.

You can download the PhpMailer also here, because I had to do some changes so that the classes are Psr-1, Psr-2 and Psr-4 conform.

Free Download

Enough of talking, let’s do some coding.

Now we can implement our error handler.
1. For production

2. For development
The development error handler is original coded by Joseph Lenton.
You will find all the code in the download link below.
Now we have two error handlers in our project, but we need to activate them in our autoloader.

That’s it. When an error occurs we will get a notification:

  • Per mail, when “mailActive” is set to true in Config.xml
  • In HipChat, when configured in Config.xml
  • Logfile, when configured in Config.xml
  • Terminal/Browser, when “error”->”printOutput” is set to true in Config.xml

The notifications are only for the production error handler. The development error handler throws the error only to screen (in html format).

Free Download

You’re welcome to make a donation if you wish :)

Leave a Reply