From 1d5fbab5157e7d50fd484be15c9c95e54d205d72 Mon Sep 17 00:00:00 2001 From: Phill Sparks Date: Wed, 16 Nov 2011 12:21:42 +0000 Subject: [PATCH] Refactor error handling to log ignored errors --- laravel/laravel.php | 50 +++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/laravel/laravel.php b/laravel/laravel.php index 43933505..f959279e 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -14,23 +14,30 @@ */ date_default_timezone_set(Config::$items['application']['timezone']); +/** + * Create the exception logging function. All of the error logging + * is routed through here to avoid duplicate code. This Closure + * will determine if the actual logging Closure should be called. + */ +$logger = function($exception) +{ + if (Config::$items['error']['log']) + { + call_user_func(Config::$items['error']['logger'], $exception); + } +}; + /** * Create the exception handler function. All of the error handlers * registered by the framework call this closure to avoid duplicate - * code. This Closure will determine if the logging Closure should - * be called, and will pass the exception to the developer defined - * handler in the configuration file. + * code. This Closure will pass the exception to the developer + * defined handler in the configuration file. */ -$handler = function($exception) +$handler = function($exception) use ($logger) { - $config = Config::$items['error']; + $logger($exception); - if ($config['log']) - { - call_user_func($config['logger'], $exception); - } - - if ($config['detail']) + if (Config::$items['error']['detail']) { echo "

Unhandled Exception

Message:

@@ -55,22 +62,29 @@ */ set_exception_handler(function($exception) use ($handler) { - $handler($exception); + $handler($exception); }); /** * Register the PHP error handler. All PHP errors will fall into this * handler, which will convert the error into an ErrorException object - * and pass the exception into the common exception handler. + * and pass the exception into the common exception handler. Suppressed + * errors are ignored and errors in the developer configured whitelist + * are silently logged. */ -set_error_handler(function($number, $error, $file, $line) +set_error_handler(function($number, $error, $file, $line) use ($logger) { - if (error_reporting() === 0 or in_array($number, Config::$items['error']['ignore'])) + if (error_reporting() === 0) { return; } - - throw new \ErrorException($error, $number, 0, $file, $line); + $exception = new \ErrorException($error, $number, 0, $file, $line); + if (in_array($number, Config::$items['error']['ignore'])) + { + $logger($exception); + return; + } + throw $exception; }); /** @@ -221,4 +235,4 @@ IoC::core('session')->save($driver); } -$response->send(); \ No newline at end of file +$response->send();