diff options
-rw-r--r-- | core/Error.php | 21 | ||||
-rw-r--r-- | core/ExceptionHandler.php | 67 | ||||
-rw-r--r-- | core/Log.php | 2 | ||||
-rw-r--r-- | core/Plugin.php | 16 | ||||
-rw-r--r-- | index.php | 3 |
5 files changed, 69 insertions, 40 deletions
diff --git a/core/Error.php b/core/Error.php index 3ec648e8f2..d31ff36de8 100644 --- a/core/Error.php +++ b/core/Error.php @@ -139,11 +139,11 @@ class Error public static function setErrorHandler() { - Piwik_AddAction('Log.formatFileMessage', array('Error', 'formatFileAndDBLogMessage')); - Piwik_AddAction('Log.formatDatabaseMessage', array('Error', 'formatFileAndDBLogMessage')); - Piwik_AddAction('Log.formatScreenMessage', array('Error', 'formatScreenMessage')); + Piwik_AddAction('Log.formatFileMessage', array('\\Piwik\\Error', 'formatFileAndDBLogMessage')); + Piwik_AddAction('Log.formatDatabaseMessage', array('\\Piwik\\Error', 'formatFileAndDBLogMessage')); + Piwik_AddAction('Log.formatScreenMessage', array('\\Piwik\\Error', 'formatScreenMessage')); - set_error_handler(array('Error', 'errorHandler')); + set_error_handler(array('\\Piwik\\Error', 'errorHandler')); } public static function errorHandler($errno, $errstr, $errfile, $errline) @@ -153,7 +153,7 @@ class Error return; } - $plugin = false; + $plugin = 'unknown'; $backtrace = ''; $bt = @debug_backtrace(); @@ -166,18 +166,13 @@ class Error . '(...) called at [' . (isset($debug['file']) ? $debug['file'] : '') . ':' . (isset($debug['line']) ? $debug['line'] : '') . ']' . "\n"; - - // try and discern the plugin name - if (empty($plugin)) { - if (preg_match("/^Piwik\\Plugins\\([a-z_]+)\\/", $debug['class'], $matches)) { - $plugin = $matches[1]; - } - } } + + $plugin = Plugin::getPluginNameFromBacktrace($bt); } $error = new Error($errno, $errstr, $errfile, $errline, $backtrace); - Log::e($plugin ?: 'unknown', $error); + Log::e($plugin, $error); switch ($errno) { case E_ERROR: 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 diff --git a/core/Log.php b/core/Log.php index 9e64512012..e98ac5e6fe 100644 --- a/core/Log.php +++ b/core/Log.php @@ -155,8 +155,6 @@ class Log private function createWriterByName($writerName) { - $self = $this; - $writer = false; if ($writerName == 'file') { $writer = array($this, 'logToFile'); diff --git a/core/Plugin.php b/core/Plugin.php index 5f0c809896..c8f48590a4 100644 --- a/core/Plugin.php +++ b/core/Plugin.php @@ -193,4 +193,18 @@ class Plugin { return $this->pluginName; } -} + + public static function getPluginNameFromBacktrace($backtrace) + { + $plugin = false; + foreach ($backtrace as $tracepoint) { + // try and discern the plugin name + if (empty($plugin)) { + if (preg_match("/^Piwik\\Plugins\\([a-z_]+)\\/", $tracepoint['class'], $matches)) { + $plugin = $matches[1]; + } + } + } + return $plugin ?: 'unknown'; + } +}
\ No newline at end of file @@ -10,6 +10,7 @@ use Piwik\FrontController; use Piwik\Error; +use Piwik\ExceptionHandler; define('PIWIK_DOCUMENT_ROOT', dirname(__FILE__) == '/' ? '' : dirname(__FILE__)); if (file_exists(PIWIK_DOCUMENT_ROOT . '/bootstrap.php')) { @@ -44,7 +45,7 @@ if (!defined('PIWIK_ENABLE_ERROR_HANDLER') || PIWIK_ENABLE_ERROR_HANDLER) { Error::setErrorHandler(); require_once PIWIK_INCLUDE_PATH . '/core/ExceptionHandler.php'; - set_exception_handler('Piwik_ExceptionHandler'); + ExceptionHandler::setUp(); } if (!defined('PIWIK_ENABLE_DISPATCH') || PIWIK_ENABLE_DISPATCH) { |