1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @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;
/**
* Contains Piwik's uncaught exception handler and log file formatting for exception
* instances.
*/
class ExceptionHandler
{
/**
* The backtrace string to use when testing.
*
* @var string
*/
public static $debugBacktraceForTests = null;
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, $tag, $datetime, $log)
{
if ($message instanceof \Exception) {
$message = sprintf("%s(%d): %s\n%s", $message->getFile(), $message->getLine(), $message->getMessage(),
self::$debugBacktraceForTests ?: $message->getTraceAsString());
$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');
}
$outputFormat = strtolower(Common::getRequestVar('format', 'html', 'string'));
$response = new ResponseBuilder($outputFormat);
$message = $response->getResponseException(new \Exception($message->getMessage()));
}
}
public static function exceptionHandler(Exception $exception)
{
Log::error($exception);
}
}
|