Registering an Error Handler

Problem

You want to add your own error handler.

Solution

Register it with the App::error() method.

App::error(function($exception)
{
    die('ERROR: '.$exception->getMessage());
});

The code above pushes the error handler to the top of the stack. Meaning it will have the first chance at processing the exception. You'll want to set it up some place early in the request lifecycle (such as app/start/global.php).

You can type-hint the exception to only handle a particular type of exception.

App::error(function(RuntimeException $exception)
{
    return View::make('error', compact('exception'));
});

Discussion

To finish error processing return a value from your handler.

This will return the value as a response to the user. If you don't return a value then the next error handler will be called. Not returning a value is handy for some functions, such as logging.

comments powered by Disqus