PHPErrorNet: Convert all errrors to exceptions, add multiple exception handlers

This class that I wrote is not complicated by any means, but it is very powerful. I see way too many deployments without proper error and exception handling. Drop this in to your project as an include in your MVCs index.php or place it in your php.ini file as an auto_prepend_file line (class will run each time a PHP script is invoked, even from CLI). I deployed this on our production machines and we found errors that no one knew were occuring, such as the default memory limit of 128M running out.

https://github.com/KyleWolfe/PHPErrorNet

A simple example of how to send an email on any uncaught exception (possibly a former uncatchable error or notice such as memory limit exceeded or undefined variable)


<?php
//bring in ErrorNet
require_once __DIR__ . '/../PHPErrorNet.php';

//add an extra error handler that emails an exception
$EmailException = function($e) {
    global $_PHPErrorNet;
    if(!class_exists('PHPMailer')) require_once __DIR__ . '/library/PHPMailer/PHPMailerAutoload.php';
    $mail = new PHPMailer;
    $mail->From = 'it@myurl.com';
    $mail->FromName = 'IT';
    $mail->addAddress('myemail@myurl.com');
    $mail->Subject = "PHPErrorNet: Uncaught Exception on " . $_PHPErrorNet->machineName . " (" . $_PHPErrorNet->env . ")";

    //do not send sensitive $_POST data across unsecured connection
    $mail->Body    = '
        <div style="font-size:11px;font-family:verdana;">
            <h1 style="font-size:16px;background:#ccc;border-bottom:1px solid #000;padding:3px;color:#fff;background:#64A2E8;">PHPErrorNet - An unhandled exception was caught by PHPErrorNet.</h1>

            <h2 style="font-size:12px;border-bottom:1px solid #000;padding:3px;color:#fff;background:#F27E7E;">The following excpeption was provided</h2>
            <p style ="padding:10px;"><pre>' . stripslashes ( $e ) . '</pre></p>

            <h2 style="font-size:12px;border-bottom:1px solid #000;padding:3px;color:#fff;background:#F27E7E;">$_SERVER Dump</h2>
            <p style ="padding:10px;"><pre>' . var_export($_SERVER, true) . '</pre></p>

            <h2 style="font-size:12px;border-bottom:1px solid #000;padding:3px;color:#fff;background:#F27E7E;">$_POST Dump</h2>
            <p style ="padding:10px;"><pre>' . var_export($_POST, true) . '</pre></p>
        </div>';
    $mail->isHTML(true);
    $mail->send();

};

//add function to list of error handlers
$_PHPErrorNet->setAdditionalHandler($EmailException);

//turn off error printing
$_PHPErrorNet->printException = false;
$_PHPErrorNet->printExceptionMessage = false;

//cause an error
$foo = $bar;

Take note that while I recomend using this class for new production machines and projects, I do not recomend dropping this into an already live project without initial testing and with a plan to remove it immediately. By default in PHP, errors such as undefined variable can go un noticed and allow the script to continue. While your script may work with these notices, this is just bad practice.

So I feel the need to bump this up to the front of the sub forum due to the fact that it received no replies and I know that this is something that is sorely missed in probably 70% of all PHP deployments (proper and an entire catch all system for your errors and exceptions). Have I made this post in a confusing manner and made it difficult to see what this little script accomplishes and its benefits? How can I bring this to the attention of younger and intermediate developers better?

We have something similar in our project here at work. It’s great to get instant notification of anything going wrong in the live system (it will log to a file and send an email).