Controller.php
6.76 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Controller is the customized base controller class.
* All controller classes for this application should extend from this base class.
*/
class Controller extends /*WRestController*/ CController
{
/**
*
*/
private $_error = array();
/**
* Initializes the controller. This method is called by the application
* before the controller starts to execute.
*/
public function init()
{
parent::init();
// Let completely bypass Yii's default error displaying mechanism
// by registering onError and onException event listeners
Yii::app()->attachEventHandler('onError',array($this,'handleError'));
Yii::app()->attachEventHandler('onException',array($this,'handleError'));
}
/**
* Handling all errors and exceptions for all controllers
* To check if there was error or exception need use construction like
*
* if ($event instanceof CExceptionEvent) { ... }
* elseif($event instanceof CErrorEvent) { ... }
*
* @param {object} event
*/
public function handleError(CEvent $event)
{
$error = ($event instanceof CExceptionEvent) ? $event->exception : $event;
$type = null;
if($event instanceof CExceptionEvent) {
if(($trace = $this->getExactTrace($error)) === null) {
$fileName = $error->getFile();
$errorLine = $error->getLine();
}
else {
$fileName = $trace['file'];
$errorLine = $trace['line'];
}
$trace = $error->getTrace();
foreach($trace as $i => $t) {
if(!isset($t['file'])) {
$trace[$i]['file']='unknown';
}
if(!isset($t['line'])) {
$trace[$i]['line']=0;
}
if(!isset($t['function'])) {
$trace[$i]['function']='unknown';
}
unset($trace[$i]['object']);
}
}
elseif($event instanceof CErrorEvent) {
$trace = debug_backtrace();
// skip the first 3 stacks as they do not tell the error position
if(count($trace) > 3) {
$trace = array_slice($trace,3);
}
$traceString = '';
foreach($trace as $i=>$t) {
if(!isset($t['file'])) {
$trace[$i]['file']='unknown';
}
if(!isset($t['line'])) {
$trace[$i]['line']=0;
}
if(!isset($t['function'])) {
$trace[$i]['function']='unknown';
}
$traceString .= "#$i {$trace[$i]['file']}({$trace[$i]['line']}): ";
if(isset($t['object']) && is_object($t['object'])) {
$traceString.=get_class($t['object']).'->';
}
$traceString.="{$trace[$i]['function']}()\n";
unset($trace[$i]['object']);
}
switch($error->code) {
case E_WARNING:
$type = 'PHP warning';
break;
case E_NOTICE:
$type = 'PHP notice';
break;
case E_USER_ERROR:
$type = 'User error';
break;
case E_USER_WARNING:
$type = 'User warning';
break;
case E_USER_NOTICE:
$type = 'User notice';
break;
case E_RECOVERABLE_ERROR:
$type = 'Recoverable error';
break;
default:
$type = 'PHP error';
}
}
else {
return;
}
$this->_error =array(
'code' => ($error instanceof CHttpException) ? $error->statusCode : 500,
'type' => $type ? $type : get_class($error),
'message' => ($error instanceof CException) ? $error->getMessage() : $error->message,
'file' => $fileName ? $fileName : $error->file,
'line' => $errorLine ? $errorLine : $error->line,
'trace' => ($error instanceof CException) ? $error->getTraceAsString() : $traceString,
'traces' => $trace
);
if(YII_DEBUG) {
$this->sendResponse($this->_error['code'], array(
'success' => false,
'error' => $this->_error
));
}
else {
if(Yii::app()->user->isGuest) {
$this->sendResponse(401, array(
'success' => false
));
}
else {
$this->sendResponse($this->_error['code'], array(
'success' => false,
'error' => array(
'message' => $this->_error['message']
))
);
}
}
// Stop rising error / exception
$event->handled = true;
}
/**
* Returns the exact trace where the problem occurs.
* @param Exception $exception the uncaught exception
* @return array the exact trace where the problem occurs
*/
protected function getExactTrace($exception)
{
$traces = $exception->getTrace();
foreach($traces as $trace)
{
// property access exception
if(isset($trace['function']) && ($trace['function']==='__get' || $trace['function']==='__set'))
return $trace;
}
return null;
}
/**
* Access filter control
*/
public function filters()
{
return array(
array(
'application.filters.AccessControl - index,authorize,error'
)
);
}
public function showJSON($params)
{
$params = !is_array($params) || empty($params) ? array() : $params;
echo "(" . CJSON::encode($params) . ")";
Yii::app()->end();
}
/**
* This is the action to handle external exceptions.
*/
public function actionError()
{
if($error=Yii::app()->errorHandler->error)
{
if(Yii::app()->request->isAjaxRequest) {
$this->showJSON(array(
"success" => false,
"error" => $error['code'],
"message" => $error['message']
));
}
else {
$this->render('error', $error);
}
}
}
}