forked from igorsantos07/yii-whoops
/
WhoopsErrorHandler.php
executable file
·134 lines (120 loc) · 3.29 KB
/
WhoopsErrorHandler.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
use Whoops\Handler\JsonResponseHandler;
use Whoops\Handler\PrettyPageHandler;
class WhoopsErrorHandler extends CErrorHandler {
/**
* Whoops instance.
* @var Whoops
*/
protected $whoops;
/**
* Page title in case of non-AJAX requests.
* If not set, will use Whoops default: "Whoops! There was an error."
* @var string
*/
public $pageTitle;
/**
* Editor to open files in. Can be one of the following:
* sublime
* emacs
* textmate
* macvim
* xdebug
* or a protocol url with the tokens {file} and {line},
* which will be replaced by the filename and line number.
* {@link https://github.com/filp/whoops/blob/master/docs/Open%20Files%20In%20An%20Editor.md}
* @var string
*/
public $editor;
protected $defaultDisabledLogRoutes = array('YiiDebugToolbarRoute');
protected $disabledLogRoutes = array();
/**
* Instantiate Whoops with the correct handlers.
*/
public function init() {
parent::init();
require 'YiiWhoopsRunner.php';
$this->whoops = new YiiWhoopsRunner();
if (Yii::app()->request->isAjaxRequest) {
$this->whoops->pushHandler(new JsonResponseHandler);
}
else {
$page_handler = new PrettyPageHandler;
if (isset($this->pageTitle)) {
$page_handler->setPageTitle($this->pageTitle);
}
if (isset($this->editor)) {
$editor = $this->editor;
switch ($editor) {
case 'sublime':
case 'emacs':
case 'textmate':
case 'macvim':
case 'xdebug':
$page_handler->setEditor($editor);
break;
default:
$page_handler->setEditor(function ($file, $line) use ($editor) {
return strtr($editor, array('{file}' => $file, '{line}' => $line));
});
break;
}
}
$this->whoops->pushHandler($page_handler);
}
}
/**
* Disables some log routes that would output stuff whenever the script finishes, trashing Whoops screen.
* @return true
*/
protected function disableLogRoutes() {
//This part verifies if the log routes to disable really exists. If none, simply returns
$disabled_routes = array_merge($this->defaultDisabledLogRoutes, $this->disabledLogRoutes);
$continue = false;
foreach ($disabled_routes as $route) {
if (class_exists($route, false)) {
$continue = true;
break;
}
}
if (!$continue) return true;
//Here we actually disable the given routes...
$total = sizeof(Yii::app()->log->routes);
for ($i = 0; $i < $total; $i++) {
foreach ($disabled_routes as $route) {
if (Yii::app()->log->routes[$i] instanceof $route) {
Yii::app()->log->routes[$i]->enabled = false;
}
}
}
return true;
}
/**
* Forwards an error to Whoops.
* @param CErrorEvent $event
*/
protected function handleError($event) {
if (!YII_DEBUG) {
parent::handleError($event);
return;
}
$this->disableLogRoutes();
try {
$this->whoops->handleError($event->code, $event->message, $event->file, $event->line);
} catch (Exception $ex) {
$this->handleException($ex);
}
}
/**
* Forwards an exception to Whoops.
* @param Exception $exception
*/
protected function handleException($exception) {
if ($exception instanceof CHttpException && $this->errorAction!==null || !YII_DEBUG) {
parent::handleException($exception);
return;
}
$this->disableLogRoutes();
$this->whoops->handleException($exception);
}
}