diff options
author | Fabian Becker <fabian.becker@uni-tuebingen.de> | 2013-09-29 21:11:12 +0400 |
---|---|---|
committer | Fabian Becker <fabian.becker@uni-tuebingen.de> | 2013-09-29 21:11:12 +0400 |
commit | 9f215ec612d32d999b6fda42d4596d0b42ffd0b0 (patch) | |
tree | e77c81e78bf71d66d4b215d9bb53764efc5906ac /core/ExceptionHandler.php | |
parent | 7d91192397f3bd1f889dfdd3b3b4554f8452372e (diff) | |
parent | c36718671a30b52355d53038383476c126e12651 (diff) |
Merge branch 'master' into registry
Conflicts:
core/ErrorHandler.php
core/ExceptionHandler.php
core/FrontController.php
core/Log.php
core/Piwik.php
Diffstat (limited to 'core/ExceptionHandler.php')
-rw-r--r-- | core/ExceptionHandler.php | 70 |
1 files changed, 45 insertions, 25 deletions
diff --git a/core/ExceptionHandler.php b/core/ExceptionHandler.php index 71c35fca45..492d321321 100644 --- a/core/ExceptionHandler.php +++ b/core/ExceptionHandler.php @@ -8,42 +8,62 @@ * @category Piwik * @package Piwik */ +namespace Piwik; + +use Piwik\Common; use Piwik\Piwik; +use Piwik\Plugin; use Piwik\Log; -use Piwik\Log\ExceptionScreenFormatter; use Piwik\FrontController; -use Piwik\Registry; +use Piwik\API\ResponseBuilder; /** - * Exception handler used to display nicely exceptions in Piwik - * - * @param Exception $exception - * @throws Exception + * Contains Piwik's uncaught exception handler and log file formatting for exception + * instances. */ -function Piwik_ExceptionHandler(Exception $exception) +class ExceptionHandler { - try { - Registry::get('logger_exception')->logEvent($exception); - } catch (Exception $e) { + /** + * The backtrace string to use when testing. + * + * @var string + */ + public static $debugBacktraceForTests = null; - 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')); + } - // 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 formatFileAndDBLogMessage(&$message, $level, $tag, $datetime, $log) + { + if ($message instanceof \Exception) { + $message = sprintf("%s(%d): %s\n%s", $message->getFile(), $message->getLine(), $message->getMessage(), + self::$debugBacktraceForTests ?: $message->getTraceAsString()); - $formatter = new ExceptionScreenFormatter(); + $message = $log->formatMessage($level, $tag, $datetime, $message); + } + } + + public static function formatScreenMessage(&$message, $level, $tag, $datetime, $log) + { + if ($message instanceof \Exception) { + if (!Common::isPhpCliMode()) { + @header('Content-Type: text/html; charset=utf-8'); + } - $message = $formatter->format($event); - $message .= "<br /><br />And this exception raised another exception \"" . $e->getMessage() . "\""; + $outputFormat = strtolower(Common::getRequestVar('format', 'html', 'string')); + $response = new ResponseBuilder($outputFormat); + $message = $response->getResponseException(new \Exception($message->getMessage())); + } + } - Piwik::exitWithErrorMessage($message); + public static function exceptionHandler(Exception $exception) + { + Log::error($exception); } } |