Skip to content
Extraits de code Groupes Projets
Valider 3433a3d3 rédigé par Benaka Moorthi's avatar Benaka Moorthi
Parcourir les fichiers

Finished converting exception handling.

parent c6350fbb
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -139,11 +139,11 @@ class Error ...@@ -139,11 +139,11 @@ class Error
public static function setErrorHandler() public static function setErrorHandler()
{ {
Piwik_AddAction('Log.formatFileMessage', array('Error', 'formatFileAndDBLogMessage')); Piwik_AddAction('Log.formatFileMessage', array('\\Piwik\\Error', 'formatFileAndDBLogMessage'));
Piwik_AddAction('Log.formatDatabaseMessage', array('Error', 'formatFileAndDBLogMessage')); Piwik_AddAction('Log.formatDatabaseMessage', array('\\Piwik\\Error', 'formatFileAndDBLogMessage'));
Piwik_AddAction('Log.formatScreenMessage', array('Error', 'formatScreenMessage')); 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) public static function errorHandler($errno, $errstr, $errfile, $errline)
...@@ -153,7 +153,7 @@ class Error ...@@ -153,7 +153,7 @@ class Error
return; return;
} }
$plugin = false; $plugin = 'unknown';
$backtrace = ''; $backtrace = '';
$bt = @debug_backtrace(); $bt = @debug_backtrace();
...@@ -166,18 +166,13 @@ class Error ...@@ -166,18 +166,13 @@ class Error
. '(...) called at [' . '(...) called at ['
. (isset($debug['file']) ? $debug['file'] : '') . ':' . (isset($debug['file']) ? $debug['file'] : '') . ':'
. (isset($debug['line']) ? $debug['line'] : '') . ']' . "\n"; . (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); $error = new Error($errno, $errstr, $errfile, $errline, $backtrace);
Log::e($plugin ?: 'unknown', $error); Log::e($plugin, $error);
switch ($errno) { switch ($errno) {
case E_ERROR: case E_ERROR:
......
...@@ -8,39 +8,60 @@ ...@@ -8,39 +8,60 @@
* @category Piwik * @category Piwik
* @package Piwik * @package Piwik
*/ */
namespace Piwik;
use Piwik\Common;
use Piwik\Piwik; use Piwik\Piwik;
use Piwik\Plugin;
use Piwik\Log; use Piwik\Log;
use Piwik\FrontController; use Piwik\FrontController;
use Piwik\API\ResponseBuilder;
/** /**
* Exception handler used to display nicely exceptions in Piwik * TODO
*
* @param Exception $exception
* @throws Exception
*/ */
function Piwik_ExceptionHandler(Exception $exception) class ExceptionHandler
{ {
try { public static function setUp()
Log::e("%s (%s): %s", array($exception->getFile(), $exception->getLine(), $exception->getMessage())); // TODO add backtrace? {
} catch (Exception $e) { Piwik_AddAction('Log.formatFileMessage', array('\\Piwik\\ExceptionHandler', 'formatFileAndDBLogMessage'));
if (FrontController::shouldRethrowException()) { Piwik_AddAction('Log.formatDatabaseMessage', array('\\Piwik\\ExceptionHandler', 'formatFileAndDBLogMessage'));
throw $exception; 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 public static function formatScreenMessage(&$message, $level, $pluginName, $datetime, $log)
// we handle the exception a la mano, but using the Logger formatting properties {
$event = array(); if ($message instanceof \Exception) {
$event['errno'] = $exception->getCode(); if (!Common::isPhpCliMode()) {
$event['message'] = $exception->getMessage(); @header('Content-Type: text/html; charset=utf-8');
$event['errfile'] = $exception->getFile(); }
$event['errline'] = $exception->getLine();
$event['backtrace'] = $exception->getTraceAsString();
$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); public static function exceptionHandler(Exception $exception)
$message .= "<br /><br />And this exception raised another exception \"" . $e->getMessage() . "\""; {
$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
...@@ -155,8 +155,6 @@ class Log ...@@ -155,8 +155,6 @@ class Log
private function createWriterByName($writerName) private function createWriterByName($writerName)
{ {
$self = $this;
$writer = false; $writer = false;
if ($writerName == 'file') { if ($writerName == 'file') {
$writer = array($this, 'logToFile'); $writer = array($this, 'logToFile');
......
...@@ -193,4 +193,18 @@ class Plugin ...@@ -193,4 +193,18 @@ class Plugin
{ {
return $this->pluginName; 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 @@ ...@@ -10,6 +10,7 @@
use Piwik\FrontController; use Piwik\FrontController;
use Piwik\Error; use Piwik\Error;
use Piwik\ExceptionHandler;
define('PIWIK_DOCUMENT_ROOT', dirname(__FILE__) == '/' ? '' : dirname(__FILE__)); define('PIWIK_DOCUMENT_ROOT', dirname(__FILE__) == '/' ? '' : dirname(__FILE__));
if (file_exists(PIWIK_DOCUMENT_ROOT . '/bootstrap.php')) { if (file_exists(PIWIK_DOCUMENT_ROOT . '/bootstrap.php')) {
...@@ -44,7 +45,7 @@ if (!defined('PIWIK_ENABLE_ERROR_HANDLER') || PIWIK_ENABLE_ERROR_HANDLER) { ...@@ -44,7 +45,7 @@ if (!defined('PIWIK_ENABLE_ERROR_HANDLER') || PIWIK_ENABLE_ERROR_HANDLER) {
Error::setErrorHandler(); Error::setErrorHandler();
require_once PIWIK_INCLUDE_PATH . '/core/ExceptionHandler.php'; require_once PIWIK_INCLUDE_PATH . '/core/ExceptionHandler.php';
set_exception_handler('Piwik_ExceptionHandler'); ExceptionHandler::setUp();
} }
if (!defined('PIWIK_ENABLE_DISPATCH') || PIWIK_ENABLE_DISPATCH) { if (!defined('PIWIK_ENABLE_DISPATCH') || PIWIK_ENABLE_DISPATCH) {
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter