/
ErrorHandler.php
285 lines (245 loc) · 8.8 KB
/
ErrorHandler.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\web;
use Craft;
use craft\events\ExceptionEvent;
use craft\helpers\App;
use craft\helpers\Json;
use craft\helpers\Template;
use GuzzleHttp\Exception\ClientException;
use Throwable;
use Twig\Error\Error as TwigError;
use Twig\Error\LoaderError as TwigLoaderError;
use Twig\Error\RuntimeError as TwigRuntimeError;
use Twig\Error\SyntaxError as TwigSyntaxError;
use Twig\Template as TwigTemplate;
use yii\base\ErrorException;
use yii\base\Exception;
use yii\base\UserException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
/**
* Class ErrorHandler
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class ErrorHandler extends \yii\web\ErrorHandler
{
/**
* @event ExceptionEvent The event that is triggered before handling an exception.
*/
public const EVENT_BEFORE_HANDLE_EXCEPTION = 'beforeHandleException';
/**
* @inheritdoc
*/
public function handleException($exception): void
{
// Fire a 'beforeHandleException' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_HANDLE_EXCEPTION)) {
$this->trigger(self::EVENT_BEFORE_HANDLE_EXCEPTION, new ExceptionEvent([
'exception' => $exception,
]));
}
// If this is a Twig Runtime exception, use the previous one instead
if ($exception instanceof TwigRuntimeError && ($previousException = $exception->getPrevious()) !== null) {
$exception = $previousException;
}
// 404?
if ($exception instanceof HttpException && $exception->statusCode === 404) {
$request = Craft::$app->getRequest();
if ($request->getIsSiteRequest() && $request->getPathInfo() === 'wp-admin') {
$exception->statusCode = 418;
}
}
parent::handleException($exception);
}
/**
* @inheritdoc
*/
public function handleError($code, $message, $file, $line): bool
{
// Because: https://bugs.php.net/bug.php?id=74980
if (str_contains($message, 'Narrowing occurred during type inference. Please file a bug report')) {
return true;
}
return parent::handleError($code, $message, $file, $line);
}
/**
* @inheritdoc
*/
public function getExceptionName($exception): ?string
{
// Yii isn't translating its own exceptions' names, so meh
if ($exception instanceof TwigError) {
if ($exception instanceof TwigSyntaxError) {
return 'Twig Syntax Error';
}
if ($exception instanceof TwigLoaderError) {
return 'Twig Template Loading Error';
}
if ($exception instanceof TwigRuntimeError) {
return 'Twig Runtime Error';
}
}
return parent::getExceptionName($exception);
}
/**
* @inheritdoc
*/
public function isCoreFile($file): bool
{
if (parent::isCoreFile($file)) {
return true;
}
$file = realpath($file);
$pathService = Craft::$app->getPath();
return str_starts_with($file, $pathService->getCompiledTemplatesPath() . DIRECTORY_SEPARATOR) ||
str_starts_with($file, $pathService->getVendorPath() . DIRECTORY_SEPARATOR . 'twig' . DIRECTORY_SEPARATOR . 'twig' . DIRECTORY_SEPARATOR) ||
$file === __DIR__ . DIRECTORY_SEPARATOR . 'twig' . DIRECTORY_SEPARATOR . 'Template.php';
}
/**
* @inheritdoc
*/
protected function renderException($exception): void
{
$request = Craft::$app->has('request', true) ? Craft::$app->getRequest() : null;
$response = Craft::$app->getResponse();
// Return JSON for JSON requests
if ($request && $request->getAcceptsJson()) {
$response->format = Response::FORMAT_JSON;
$includeFullInfo = $this->_showExceptionView();
$message = ($includeFullInfo || $exception instanceof UserException)
? $exception->getMessage()
: Craft::t('app', 'A server error occurred.');
$response->data = [
'name' => ($exception instanceof Exception || $exception instanceof ErrorException) ? $exception->getName() : 'Exception',
'message' => $message,
'code' => $exception->getCode(),
// TODO: remove in v5; error message should only be in `message`
'error' => $message,
];
if ($exception instanceof HttpException) {
$response->data['status'] = $exception->statusCode;
}
if ($includeFullInfo) {
$response->data += $this->_exceptionAsArray($exception, false);
}
// Override the status code and error message if this is a Guzzle client exception
if ($exception instanceof ClientException) {
$response->setStatusCode($exception->getCode());
if (($guzzleResponse = $exception->getResponse()) !== null) {
// TODO: review for v5
$body = Json::decodeIfJson((string)$guzzleResponse->getBody());
if (isset($body['message'])) {
$response->data['error'] = $body['message'];
}
}
} else {
$response->setStatusCodeByException($exception);
}
$response->send();
return;
}
// Set the response format to HTML if it's still set to raw
if ($response->format === Response::FORMAT_RAW) {
$response->format = Response::FORMAT_HTML;
}
// Show a broken image for image requests
if (
$exception instanceof NotFoundHttpException &&
$request &&
$request->getAcceptsImage() &&
Craft::$app->getConfig()->getGeneral()->brokenImagePath
) {
$this->errorAction = 'app/broken-image';
}
// Show the full exception view for all exceptions when Dev Mode is enabled (don't skip `UserException`s)
// or if the user is an admin and has indicated they want to see it
elseif ($this->_showExceptionView()) {
$this->errorAction = null;
$this->errorView = $this->exceptionView;
}
parent::renderException($exception);
}
private function _exceptionAsArray(Throwable $exception, bool $withMessage)
{
$array = [
'exception' => get_class($exception),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => array_map(function($step) {
unset($step['args']);
return $step;
}, $exception->getTrace()),
];
if ($withMessage) {
$array = ['message' => $exception->getMessage()] + $array;
}
$prev = $exception->getPrevious();
if ($prev !== null) {
$array['previous'] = $this->_exceptionAsArray($prev, true);
}
return $array;
}
/**
* @inheritdoc
*/
protected function getTypeUrl($class, $method): ?string
{
$url = parent::getTypeUrl($class, $method);
if ($url === null) {
if (str_starts_with($class, '__TwigTemplate_')) {
$class = TwigTemplate::class;
}
if (str_starts_with($class, 'Twig\\')) {
$url = "http://twig.sensiolabs.org/api/2.x/$class.html";
if ($method) {
$url .= "#method_$method";
}
}
}
return $url;
}
/**
* @inheritdoc
*/
public function renderCallStackItem($file, $line, $class, $method, $args, $index): string
{
$templateInfo = Template::resolveTemplatePathAndLine($file ?? '', $line);
if ($templateInfo !== false) {
[$file, $line] = $templateInfo;
}
return parent::renderCallStackItem($file, $line, $class, $method, $args, $index);
}
/**
* Returns whether the full exception view should be shown.
*
* @return bool
*/
private function _showExceptionView(): bool
{
if (App::devMode()) {
return true;
}
$user = Craft::$app->getUser()->getIdentity();
return (
$user &&
$user->admin &&
$user->getPreference('showExceptionView')
);
}
/**
* @inheritdoc
* @since 3.4.10
*/
protected function shouldRenderSimpleHtml(): bool
{
/** @phpstan-ignore-next-line */
return YII_ENV_TEST || (Craft::$app->has('request', true) && Craft::$app->request->getIsAjax());
}
}