Tag Archives: Errors

PHP: Catch all exceptions and errors in a normalized and consistent matter

For my small website myhymnal.net I wanted a nice error handler that caught all errors and presented them as nice error pages. The problem is that you have at least three kinds of errors which you have to handle in PHP. Uncaught exceptions, regular errors and fatal errors. After some experimenting I think I’ve found a way to catch them all in a close to consistent matter so that they are easy to deal with.

error_reporting(ENV === 'dev' ? E_ALL : 0);

set_error_handler("error_handler");
set_exception_handler("error_handler");
register_shutdown_function("error_handler");

function error_handler()
{
    // Check for unhandled errors (fatal shutdown)
    $e = error_get_last();

    // If none, check function args (error handler)
    if($e === null)
        $e = func_get_args();

    // Return if no error
    if(empty($e))
        return;

    // "Normalize" exceptions (exception handler)
    if($e[0] instanceof Exception)
    {
        call_user_func_array(__FUNCTION__, array(
            $e[0]->getCode(),
            $e[0]->getMessage(),
            $e[0]->getFile(),
            $e[0]->getLine(),
            $e[0]));
        return;
    }

    $e = array_combine(array('number', 'message', 'file', 'line', 'context'), array_pad($e, 5, null));
   
    var_dump($e);
    exit;
}

This way the only irregularity should be the last property, context. In cases of errors this will be an array with variables in the context where the error occurred, while for exceptions I’m setting it to the Exception itself. Normally you’d then pass $e to something that would format the information nicely to the user, do some logging or whatever.

Skype crash on Windows with offset 006eb5e2

Probably an obscure issue I’ll never experience again, but in case it does, I found a solution that worked in a thread on Yahoo Answers.

Run one of the commands below in a Command Prompt or just stick it into the Run dialog (win + r). Then restart Skype *sigh*… 🙄

Windows XP

> del "%userprofile%\Application Data\Skype\shared.xml"

Windows 7 (and Vista?)

> del "%appdata%\Skype\shared.xml"
Note: You may of course also find and delete the file by using Windows Explorer. I just prefer a quick one-off command 🙂

/a

How to ignore “find: cannot read dir /foo: Permission denied”

If you run the following command on a Unix system, depending on your permissions and other stuff, you might see a lot of errors.

$ find / -name "tar"

In my case I got 8 regular findings and 63 lines of “find: cannot read dir /foo: Permission denied”. Quite a lot of noise I really don’t care about. Using my new knowledge about streams it is easy to get rid of all that though. Just pipe stderr into /dev/null.

Continue reading How to ignore “find: cannot read dir /foo: Permission denied”