diff options
author | Benaka Moorthi <benaka.moorthi@gmail.com> | 2013-09-24 10:15:11 +0400 |
---|---|---|
committer | Benaka Moorthi <benaka.moorthi@gmail.com> | 2013-09-24 10:15:11 +0400 |
commit | 3433a3d353a86c4c001693dedeea102c06b4c19e (patch) | |
tree | 902d8bce481a6bad1f3d9670bd4a3e287e5b5473 /core/ExceptionHandler.php | |
parent | c6350fbba5b80a89aaef2b660632c171dfb9a33a (diff) |
Finished converting exception handling.
Diffstat (limited to 'core/ExceptionHandler.php')
-rw-r--r-- | core/ExceptionHandler.php | 67 |
1 files changed, 44 insertions, 23 deletions
diff --git a/core/ExceptionHandler.php b/core/ExceptionHandler.php index ac632f1660..62b0bd2a77 100644 --- a/core/ExceptionHandler.php +++ b/core/ExceptionHandler.php @@ -8,39 +8,60 @@ * @category Piwik * @package Piwik */ +namespace Piwik; + +use Piwik\Common; use Piwik\Piwik; +use Piwik\Plugin; use Piwik\Log; use Piwik\FrontController; +use Piwik\API\ResponseBuilder; /** - * Exception handler used to display nicely exceptions in Piwik - * - * @param Exception $exception - * @throws Exception + * TODO */ -function Piwik_ExceptionHandler(Exception $exception) +class ExceptionHandler { - try { - Log::e("%s (%s): %s", array($exception->getFile(), $exception->getLine(), $exception->getMessage())); // TODO add backtrace? - } catch (Exception $e) { - if (FrontController::shouldRethrowException()) { - throw $exception; + public static function setUp() + { + Piwik_AddAction('Log.formatFileMessage', array('\\Piwik\\ExceptionHandler', 'formatFileAndDBLogMessage')); + Piwik_AddAction('Log.formatDatabaseMessage', array('\\Piwik\\ExceptionHandler', 'formatFileAndDBLogMessage')); + Piwik_AddAction('Log.formatScreenMessage', array('\\Piwik\\ExceptionHandler', 'formatScreenMessage')); + + set_exception_handler(array('\\Piwik\\ExceptionHandler', 'exceptionHandler')); + } + + public static function formatFileAndDBLogMessage(&$message, $level, $pluginName, $datetime, $log) + { + if ($message instanceof \Exception) { + $message = sprintf("%s(%d): %s\n%s", $message->getFile(), $message->getLine(), $message->getMessage(), + $message->getTraceAsString()); + + $message = $log->formatMessage($level, $pluginName, $datetime, $message); } + } - // case when the exception is raised before the logger being ready - // we handle the exception a la mano, but using the Logger formatting properties - $event = array(); - $event['errno'] = $exception->getCode(); - $event['message'] = $exception->getMessage(); - $event['errfile'] = $exception->getFile(); - $event['errline'] = $exception->getLine(); - $event['backtrace'] = $exception->getTraceAsString(); + public static function formatScreenMessage(&$message, $level, $pluginName, $datetime, $log) + { + if ($message instanceof \Exception) { + if (!Common::isPhpCliMode()) { + @header('Content-Type: text/html; charset=utf-8'); + } - $formatter = new ExceptionScreenFormatter(); + $outputFormat = strtolower(Common::getRequestVar('format', 'html', 'string')); + $response = new ResponseBuilder($outputFormat); + $message = $response->getResponseException(new \Exception($message->getMessage())); + } + } - $message = $formatter->format($event); - $message .= "<br /><br />And this exception raised another exception \"" . $e->getMessage() . "\""; + public static function exceptionHandler(Exception $exception) + { + $plugin = Plugin::getPluginNameFromBacktrace($exception->getTrace()); + Log::e($plugin, $exception); - Piwik::exitWithErrorMessage($message); + // TODO: what about this code? + /*if (FrontController::shouldRethrowException()) { + throw $exception; + }*/ } -} +}
\ No newline at end of file |