1 | <?php |
||
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() |
|
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() |
|
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() |
|
210 | } |
||
211 |