1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\web; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\Action; |
12
|
|
|
use yii\base\Exception; |
13
|
|
|
use yii\base\UserException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ErrorAction displays application errors using a specified view. |
17
|
|
|
* |
18
|
|
|
* To use ErrorAction, you need to do the following steps: |
19
|
|
|
* |
20
|
|
|
* First, declare an action of ErrorAction type in the `actions()` method of your `SiteController` |
21
|
|
|
* class (or whatever controller you prefer), like the following: |
22
|
|
|
* |
23
|
|
|
* ```php |
24
|
|
|
* public function actions() |
25
|
|
|
* { |
26
|
|
|
* return [ |
27
|
|
|
* 'error' => ['class' => \yii\web\ErrorAction::class, |
28
|
|
|
* ]; |
29
|
|
|
* } |
30
|
|
|
* ``` |
31
|
|
|
* |
32
|
|
|
* Then, create a view file for this action. If the route of your error action is `site/error`, then |
33
|
|
|
* the view file should be `views/site/error.php`. In this view file, the following variables are available: |
34
|
|
|
* |
35
|
|
|
* - `$name`: the error name |
36
|
|
|
* - `$message`: the error message |
37
|
|
|
* - `$exception`: the exception being handled |
38
|
|
|
* |
39
|
|
|
* Finally, configure the "errorHandler" application component as follows, |
40
|
|
|
* |
41
|
|
|
* ```php |
42
|
|
|
* 'errorHandler' => [ |
43
|
|
|
* 'errorAction' => 'site/error', |
44
|
|
|
* ] |
45
|
|
|
* ``` |
46
|
|
|
* |
47
|
|
|
* @author Qiang Xue <[email protected]> |
48
|
|
|
* @author Dmitry Naumenko <[email protected]> |
49
|
|
|
* @since 2.0 |
50
|
|
|
*/ |
51
|
|
|
class ErrorAction extends Action |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* @var string the view file to be rendered. If not set, it will take the value of [[id]]. |
55
|
|
|
* That means, if you name the action as "error" in "SiteController", then the view name |
56
|
|
|
* would be "error", and the corresponding view file would be "views/site/error.php". |
57
|
|
|
*/ |
58
|
|
|
public $view; |
59
|
|
|
/** |
60
|
|
|
* @var string the name of the error when the exception name cannot be determined. |
61
|
|
|
* Defaults to "Error". |
62
|
|
|
*/ |
63
|
|
|
public $defaultName; |
64
|
|
|
/** |
65
|
|
|
* @var string the message to be displayed when the exception message contains sensitive information. |
66
|
|
|
* Defaults to "An internal server error occurred.". |
67
|
|
|
*/ |
68
|
|
|
public $defaultMessage; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var \Exception the exception object, normally is filled on [[init()]] method call. |
72
|
|
|
* @see [[findException()]] to know default way of obtaining exception. |
73
|
|
|
* @since 2.0.11 |
74
|
|
|
*/ |
75
|
|
|
protected $exception; |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
6 |
|
public function init() |
82
|
|
|
{ |
83
|
6 |
|
$this->exception = $this->findException(); |
84
|
|
|
|
85
|
6 |
|
if ($this->defaultMessage === null) { |
86
|
5 |
|
$this->defaultMessage = Yii::t('yii', 'An internal server error occurred.'); |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
if ($this->defaultName === null) { |
90
|
5 |
|
$this->defaultName = Yii::t('yii', 'Error'); |
91
|
|
|
} |
92
|
6 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Runs the action. |
96
|
|
|
* |
97
|
|
|
* @return string result content |
98
|
|
|
*/ |
99
|
5 |
|
public function run() |
100
|
|
|
{ |
101
|
5 |
|
Yii::$app->getResponse()->setStatusCodeByException($this->exception); |
102
|
|
|
|
103
|
5 |
|
if (Yii::$app->getRequest()->getIsAjax()) { |
104
|
|
|
return $this->renderAjaxResponse(); |
105
|
|
|
} |
106
|
|
|
|
107
|
5 |
|
return $this->renderHtmlResponse(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Builds string that represents the exception. |
112
|
|
|
* Normally used to generate a response to AJAX request. |
113
|
|
|
* @return string |
114
|
|
|
* @since 2.0.11 |
115
|
|
|
*/ |
116
|
|
|
protected function renderAjaxResponse() |
117
|
|
|
{ |
118
|
|
|
return $this->getExceptionName() . ': ' . $this->getExceptionMessage(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Renders a view that represents the exception. |
123
|
|
|
* @return string |
124
|
|
|
* @since 2.0.11 |
125
|
|
|
*/ |
126
|
6 |
|
protected function renderHtmlResponse() |
127
|
|
|
{ |
128
|
6 |
|
return $this->controller->render($this->view ?: $this->id, $this->getViewRenderParams()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Builds array of parameters that will be passed to the view. |
133
|
|
|
* @return array |
134
|
|
|
* @since 2.0.11 |
135
|
|
|
*/ |
136
|
6 |
|
protected function getViewRenderParams() |
137
|
|
|
{ |
138
|
|
|
return [ |
139
|
6 |
|
'name' => $this->getExceptionName(), |
140
|
6 |
|
'message' => $this->getExceptionMessage(), |
141
|
6 |
|
'exception' => $this->exception, |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Gets exception from the [[yii\web\ErrorHandler|ErrorHandler]] component. |
147
|
|
|
* In case there is no exception in the component, treat as the action has been invoked |
148
|
|
|
* not from error handler, but by direct route, so '404 Not Found' error will be displayed. |
149
|
|
|
* @return \Exception |
150
|
|
|
* @since 2.0.11 |
151
|
|
|
*/ |
152
|
6 |
|
protected function findException() |
153
|
|
|
{ |
154
|
6 |
|
if (($exception = Yii::$app->getErrorHandler()->exception) === null) { |
155
|
2 |
|
$exception = new NotFoundHttpException(Yii::t('yii', 'Page not found.')); |
156
|
|
|
} |
157
|
|
|
|
158
|
6 |
|
return $exception; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Gets the code from the [[exception]]. |
163
|
|
|
* @return mixed |
164
|
|
|
* @since 2.0.11 |
165
|
|
|
*/ |
166
|
6 |
|
protected function getExceptionCode() |
167
|
|
|
{ |
168
|
6 |
|
if ($this->exception instanceof HttpException) { |
169
|
2 |
|
return $this->exception->statusCode; |
170
|
|
|
} |
171
|
|
|
|
172
|
4 |
|
return $this->exception->getCode(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns the exception name, followed by the code (if present). |
177
|
|
|
* |
178
|
|
|
* @return string |
179
|
|
|
* @since 2.0.11 |
180
|
|
|
*/ |
181
|
6 |
|
protected function getExceptionName() |
182
|
|
|
{ |
183
|
6 |
|
if ($this->exception instanceof Exception) { |
184
|
4 |
|
$name = $this->exception->getName(); |
185
|
|
|
} else { |
186
|
2 |
|
$name = $this->defaultName; |
187
|
|
|
} |
188
|
|
|
|
189
|
6 |
|
if ($code = $this->getExceptionCode()) { |
190
|
2 |
|
$name .= " (#$code)"; |
191
|
|
|
} |
192
|
|
|
|
193
|
6 |
|
return $name; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Returns the [[exception]] message for [[yii\base\UserException]] only. |
198
|
|
|
* For other cases [[defaultMessage]] will be returned. |
199
|
|
|
* @return string |
200
|
|
|
* @since 2.0.11 |
201
|
|
|
*/ |
202
|
6 |
|
protected function getExceptionMessage() |
203
|
|
|
{ |
204
|
6 |
|
if ($this->exception instanceof UserException) { |
205
|
3 |
|
return $this->exception->getMessage(); |
206
|
|
|
} |
207
|
|
|
|
208
|
3 |
|
return $this->defaultMessage; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|