Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Debug;
|
Chris@0
|
13
|
Chris@17
|
14 use Psr\Log\LoggerInterface;
|
Chris@0
|
15 use Psr\Log\LogLevel;
|
Chris@0
|
16 use Symfony\Component\Debug\Exception\ContextErrorException;
|
Chris@0
|
17 use Symfony\Component\Debug\Exception\FatalErrorException;
|
Chris@0
|
18 use Symfony\Component\Debug\Exception\FatalThrowableError;
|
Chris@0
|
19 use Symfony\Component\Debug\Exception\OutOfMemoryException;
|
Chris@0
|
20 use Symfony\Component\Debug\Exception\SilencedErrorContext;
|
Chris@17
|
21 use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
|
Chris@17
|
22 use Symfony\Component\Debug\FatalErrorHandler\FatalErrorHandlerInterface;
|
Chris@0
|
23 use Symfony\Component\Debug\FatalErrorHandler\UndefinedFunctionFatalErrorHandler;
|
Chris@0
|
24 use Symfony\Component\Debug\FatalErrorHandler\UndefinedMethodFatalErrorHandler;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * A generic ErrorHandler for the PHP engine.
|
Chris@0
|
28 *
|
Chris@0
|
29 * Provides five bit fields that control how errors are handled:
|
Chris@0
|
30 * - thrownErrors: errors thrown as \ErrorException
|
Chris@0
|
31 * - loggedErrors: logged errors, when not @-silenced
|
Chris@0
|
32 * - scopedErrors: errors thrown or logged with their local context
|
Chris@0
|
33 * - tracedErrors: errors logged with their stack trace
|
Chris@0
|
34 * - screamedErrors: never @-silenced errors
|
Chris@0
|
35 *
|
Chris@0
|
36 * Each error level can be logged by a dedicated PSR-3 logger object.
|
Chris@0
|
37 * Screaming only applies to logging.
|
Chris@0
|
38 * Throwing takes precedence over logging.
|
Chris@0
|
39 * Uncaught exceptions are logged as E_ERROR.
|
Chris@0
|
40 * E_DEPRECATED and E_USER_DEPRECATED levels never throw.
|
Chris@0
|
41 * E_RECOVERABLE_ERROR and E_USER_ERROR levels always throw.
|
Chris@0
|
42 * Non catchable errors that can be detected at shutdown time are logged when the scream bit field allows so.
|
Chris@0
|
43 * As errors have a performance cost, repeated errors are all logged, so that the developer
|
Chris@0
|
44 * can see them and weight them as more important to fix than others of the same level.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @author Nicolas Grekas <p@tchwork.com>
|
Chris@0
|
47 * @author Grégoire Pineau <lyrixx@lyrixx.info>
|
Chris@0
|
48 */
|
Chris@0
|
49 class ErrorHandler
|
Chris@0
|
50 {
|
Chris@17
|
51 private $levels = [
|
Chris@0
|
52 E_DEPRECATED => 'Deprecated',
|
Chris@0
|
53 E_USER_DEPRECATED => 'User Deprecated',
|
Chris@0
|
54 E_NOTICE => 'Notice',
|
Chris@0
|
55 E_USER_NOTICE => 'User Notice',
|
Chris@0
|
56 E_STRICT => 'Runtime Notice',
|
Chris@0
|
57 E_WARNING => 'Warning',
|
Chris@0
|
58 E_USER_WARNING => 'User Warning',
|
Chris@0
|
59 E_COMPILE_WARNING => 'Compile Warning',
|
Chris@0
|
60 E_CORE_WARNING => 'Core Warning',
|
Chris@0
|
61 E_USER_ERROR => 'User Error',
|
Chris@0
|
62 E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
|
Chris@0
|
63 E_COMPILE_ERROR => 'Compile Error',
|
Chris@0
|
64 E_PARSE => 'Parse Error',
|
Chris@0
|
65 E_ERROR => 'Error',
|
Chris@0
|
66 E_CORE_ERROR => 'Core Error',
|
Chris@17
|
67 ];
|
Chris@0
|
68
|
Chris@17
|
69 private $loggers = [
|
Chris@17
|
70 E_DEPRECATED => [null, LogLevel::INFO],
|
Chris@17
|
71 E_USER_DEPRECATED => [null, LogLevel::INFO],
|
Chris@17
|
72 E_NOTICE => [null, LogLevel::WARNING],
|
Chris@17
|
73 E_USER_NOTICE => [null, LogLevel::WARNING],
|
Chris@17
|
74 E_STRICT => [null, LogLevel::WARNING],
|
Chris@17
|
75 E_WARNING => [null, LogLevel::WARNING],
|
Chris@17
|
76 E_USER_WARNING => [null, LogLevel::WARNING],
|
Chris@17
|
77 E_COMPILE_WARNING => [null, LogLevel::WARNING],
|
Chris@17
|
78 E_CORE_WARNING => [null, LogLevel::WARNING],
|
Chris@17
|
79 E_USER_ERROR => [null, LogLevel::CRITICAL],
|
Chris@17
|
80 E_RECOVERABLE_ERROR => [null, LogLevel::CRITICAL],
|
Chris@17
|
81 E_COMPILE_ERROR => [null, LogLevel::CRITICAL],
|
Chris@17
|
82 E_PARSE => [null, LogLevel::CRITICAL],
|
Chris@17
|
83 E_ERROR => [null, LogLevel::CRITICAL],
|
Chris@17
|
84 E_CORE_ERROR => [null, LogLevel::CRITICAL],
|
Chris@17
|
85 ];
|
Chris@0
|
86
|
Chris@0
|
87 private $thrownErrors = 0x1FFF; // E_ALL - E_DEPRECATED - E_USER_DEPRECATED
|
Chris@0
|
88 private $scopedErrors = 0x1FFF; // E_ALL - E_DEPRECATED - E_USER_DEPRECATED
|
Chris@0
|
89 private $tracedErrors = 0x77FB; // E_ALL - E_STRICT - E_PARSE
|
Chris@0
|
90 private $screamedErrors = 0x55; // E_ERROR + E_CORE_ERROR + E_COMPILE_ERROR + E_PARSE
|
Chris@0
|
91 private $loggedErrors = 0;
|
Chris@0
|
92 private $traceReflector;
|
Chris@0
|
93
|
Chris@0
|
94 private $isRecursive = 0;
|
Chris@0
|
95 private $isRoot = false;
|
Chris@0
|
96 private $exceptionHandler;
|
Chris@0
|
97 private $bootstrappingLogger;
|
Chris@0
|
98
|
Chris@0
|
99 private static $reservedMemory;
|
Chris@17
|
100 private static $stackedErrors = [];
|
Chris@17
|
101 private static $stackedErrorLevels = [];
|
Chris@0
|
102 private static $toStringException = null;
|
Chris@17
|
103 private static $silencedErrorCache = [];
|
Chris@12
|
104 private static $silencedErrorCount = 0;
|
Chris@0
|
105 private static $exitCode = 0;
|
Chris@0
|
106
|
Chris@0
|
107 /**
|
Chris@0
|
108 * Registers the error handler.
|
Chris@0
|
109 *
|
Chris@0
|
110 * @param self|null $handler The handler to register
|
Chris@0
|
111 * @param bool $replace Whether to replace or not any existing handler
|
Chris@0
|
112 *
|
Chris@0
|
113 * @return self The registered error handler
|
Chris@0
|
114 */
|
Chris@0
|
115 public static function register(self $handler = null, $replace = true)
|
Chris@0
|
116 {
|
Chris@0
|
117 if (null === self::$reservedMemory) {
|
Chris@0
|
118 self::$reservedMemory = str_repeat('x', 10240);
|
Chris@0
|
119 register_shutdown_function(__CLASS__.'::handleFatalError');
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 if ($handlerIsNew = null === $handler) {
|
Chris@0
|
123 $handler = new static();
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@17
|
126 if (null === $prev = set_error_handler([$handler, 'handleError'])) {
|
Chris@0
|
127 restore_error_handler();
|
Chris@0
|
128 // Specifying the error types earlier would expose us to https://bugs.php.net/63206
|
Chris@17
|
129 set_error_handler([$handler, 'handleError'], $handler->thrownErrors | $handler->loggedErrors);
|
Chris@0
|
130 $handler->isRoot = true;
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@17
|
133 if ($handlerIsNew && \is_array($prev) && $prev[0] instanceof self) {
|
Chris@0
|
134 $handler = $prev[0];
|
Chris@0
|
135 $replace = false;
|
Chris@0
|
136 }
|
Chris@12
|
137 if (!$replace && $prev) {
|
Chris@12
|
138 restore_error_handler();
|
Chris@17
|
139 $handlerIsRegistered = \is_array($prev) && $handler === $prev[0];
|
Chris@13
|
140 } else {
|
Chris@13
|
141 $handlerIsRegistered = true;
|
Chris@12
|
142 }
|
Chris@17
|
143 if (\is_array($prev = set_exception_handler([$handler, 'handleException'])) && $prev[0] instanceof self) {
|
Chris@12
|
144 restore_exception_handler();
|
Chris@13
|
145 if (!$handlerIsRegistered) {
|
Chris@13
|
146 $handler = $prev[0];
|
Chris@13
|
147 } elseif ($handler !== $prev[0] && $replace) {
|
Chris@17
|
148 set_exception_handler([$handler, 'handleException']);
|
Chris@13
|
149 $p = $prev[0]->setExceptionHandler(null);
|
Chris@13
|
150 $handler->setExceptionHandler($p);
|
Chris@13
|
151 $prev[0]->setExceptionHandler($p);
|
Chris@13
|
152 }
|
Chris@0
|
153 } else {
|
Chris@12
|
154 $handler->setExceptionHandler($prev);
|
Chris@0
|
155 }
|
Chris@0
|
156
|
Chris@0
|
157 $handler->throwAt(E_ALL & $handler->thrownErrors, true);
|
Chris@0
|
158
|
Chris@0
|
159 return $handler;
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 public function __construct(BufferingLogger $bootstrappingLogger = null)
|
Chris@0
|
163 {
|
Chris@0
|
164 if ($bootstrappingLogger) {
|
Chris@0
|
165 $this->bootstrappingLogger = $bootstrappingLogger;
|
Chris@0
|
166 $this->setDefaultLogger($bootstrappingLogger);
|
Chris@0
|
167 }
|
Chris@0
|
168 $this->traceReflector = new \ReflectionProperty('Exception', 'trace');
|
Chris@0
|
169 $this->traceReflector->setAccessible(true);
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 /**
|
Chris@0
|
173 * Sets a logger to non assigned errors levels.
|
Chris@0
|
174 *
|
Chris@0
|
175 * @param LoggerInterface $logger A PSR-3 logger to put as default for the given levels
|
Chris@0
|
176 * @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
|
Chris@0
|
177 * @param bool $replace Whether to replace or not any existing logger
|
Chris@0
|
178 */
|
Chris@0
|
179 public function setDefaultLogger(LoggerInterface $logger, $levels = E_ALL, $replace = false)
|
Chris@0
|
180 {
|
Chris@17
|
181 $loggers = [];
|
Chris@0
|
182
|
Chris@17
|
183 if (\is_array($levels)) {
|
Chris@0
|
184 foreach ($levels as $type => $logLevel) {
|
Chris@0
|
185 if (empty($this->loggers[$type][0]) || $replace || $this->loggers[$type][0] === $this->bootstrappingLogger) {
|
Chris@17
|
186 $loggers[$type] = [$logger, $logLevel];
|
Chris@0
|
187 }
|
Chris@0
|
188 }
|
Chris@0
|
189 } else {
|
Chris@0
|
190 if (null === $levels) {
|
Chris@0
|
191 $levels = E_ALL;
|
Chris@0
|
192 }
|
Chris@0
|
193 foreach ($this->loggers as $type => $log) {
|
Chris@0
|
194 if (($type & $levels) && (empty($log[0]) || $replace || $log[0] === $this->bootstrappingLogger)) {
|
Chris@0
|
195 $log[0] = $logger;
|
Chris@0
|
196 $loggers[$type] = $log;
|
Chris@0
|
197 }
|
Chris@0
|
198 }
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 $this->setLoggers($loggers);
|
Chris@0
|
202 }
|
Chris@0
|
203
|
Chris@0
|
204 /**
|
Chris@0
|
205 * Sets a logger for each error level.
|
Chris@0
|
206 *
|
Chris@0
|
207 * @param array $loggers Error levels to [LoggerInterface|null, LogLevel::*] map
|
Chris@0
|
208 *
|
Chris@0
|
209 * @return array The previous map
|
Chris@0
|
210 *
|
Chris@0
|
211 * @throws \InvalidArgumentException
|
Chris@0
|
212 */
|
Chris@0
|
213 public function setLoggers(array $loggers)
|
Chris@0
|
214 {
|
Chris@0
|
215 $prevLogged = $this->loggedErrors;
|
Chris@0
|
216 $prev = $this->loggers;
|
Chris@17
|
217 $flush = [];
|
Chris@0
|
218
|
Chris@0
|
219 foreach ($loggers as $type => $log) {
|
Chris@0
|
220 if (!isset($prev[$type])) {
|
Chris@0
|
221 throw new \InvalidArgumentException('Unknown error type: '.$type);
|
Chris@0
|
222 }
|
Chris@17
|
223 if (!\is_array($log)) {
|
Chris@17
|
224 $log = [$log];
|
Chris@18
|
225 } elseif (!\array_key_exists(0, $log)) {
|
Chris@0
|
226 throw new \InvalidArgumentException('No logger provided');
|
Chris@0
|
227 }
|
Chris@0
|
228 if (null === $log[0]) {
|
Chris@0
|
229 $this->loggedErrors &= ~$type;
|
Chris@0
|
230 } elseif ($log[0] instanceof LoggerInterface) {
|
Chris@0
|
231 $this->loggedErrors |= $type;
|
Chris@0
|
232 } else {
|
Chris@0
|
233 throw new \InvalidArgumentException('Invalid logger provided');
|
Chris@0
|
234 }
|
Chris@0
|
235 $this->loggers[$type] = $log + $prev[$type];
|
Chris@0
|
236
|
Chris@0
|
237 if ($this->bootstrappingLogger && $prev[$type][0] === $this->bootstrappingLogger) {
|
Chris@0
|
238 $flush[$type] = $type;
|
Chris@0
|
239 }
|
Chris@0
|
240 }
|
Chris@0
|
241 $this->reRegister($prevLogged | $this->thrownErrors);
|
Chris@0
|
242
|
Chris@0
|
243 if ($flush) {
|
Chris@0
|
244 foreach ($this->bootstrappingLogger->cleanLogs() as $log) {
|
Chris@0
|
245 $type = $log[2]['exception'] instanceof \ErrorException ? $log[2]['exception']->getSeverity() : E_ERROR;
|
Chris@0
|
246 if (!isset($flush[$type])) {
|
Chris@0
|
247 $this->bootstrappingLogger->log($log[0], $log[1], $log[2]);
|
Chris@0
|
248 } elseif ($this->loggers[$type][0]) {
|
Chris@0
|
249 $this->loggers[$type][0]->log($this->loggers[$type][1], $log[1], $log[2]);
|
Chris@0
|
250 }
|
Chris@0
|
251 }
|
Chris@0
|
252 }
|
Chris@0
|
253
|
Chris@0
|
254 return $prev;
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * Sets a user exception handler.
|
Chris@0
|
259 *
|
Chris@0
|
260 * @param callable $handler A handler that will be called on Exception
|
Chris@0
|
261 *
|
Chris@0
|
262 * @return callable|null The previous exception handler
|
Chris@0
|
263 */
|
Chris@0
|
264 public function setExceptionHandler(callable $handler = null)
|
Chris@0
|
265 {
|
Chris@0
|
266 $prev = $this->exceptionHandler;
|
Chris@0
|
267 $this->exceptionHandler = $handler;
|
Chris@0
|
268
|
Chris@0
|
269 return $prev;
|
Chris@0
|
270 }
|
Chris@0
|
271
|
Chris@0
|
272 /**
|
Chris@0
|
273 * Sets the PHP error levels that throw an exception when a PHP error occurs.
|
Chris@0
|
274 *
|
Chris@0
|
275 * @param int $levels A bit field of E_* constants for thrown errors
|
Chris@0
|
276 * @param bool $replace Replace or amend the previous value
|
Chris@0
|
277 *
|
Chris@0
|
278 * @return int The previous value
|
Chris@0
|
279 */
|
Chris@0
|
280 public function throwAt($levels, $replace = false)
|
Chris@0
|
281 {
|
Chris@0
|
282 $prev = $this->thrownErrors;
|
Chris@0
|
283 $this->thrownErrors = ($levels | E_RECOVERABLE_ERROR | E_USER_ERROR) & ~E_USER_DEPRECATED & ~E_DEPRECATED;
|
Chris@0
|
284 if (!$replace) {
|
Chris@0
|
285 $this->thrownErrors |= $prev;
|
Chris@0
|
286 }
|
Chris@0
|
287 $this->reRegister($prev | $this->loggedErrors);
|
Chris@0
|
288
|
Chris@0
|
289 return $prev;
|
Chris@0
|
290 }
|
Chris@0
|
291
|
Chris@0
|
292 /**
|
Chris@0
|
293 * Sets the PHP error levels for which local variables are preserved.
|
Chris@0
|
294 *
|
Chris@0
|
295 * @param int $levels A bit field of E_* constants for scoped errors
|
Chris@0
|
296 * @param bool $replace Replace or amend the previous value
|
Chris@0
|
297 *
|
Chris@0
|
298 * @return int The previous value
|
Chris@0
|
299 */
|
Chris@0
|
300 public function scopeAt($levels, $replace = false)
|
Chris@0
|
301 {
|
Chris@0
|
302 $prev = $this->scopedErrors;
|
Chris@0
|
303 $this->scopedErrors = (int) $levels;
|
Chris@0
|
304 if (!$replace) {
|
Chris@0
|
305 $this->scopedErrors |= $prev;
|
Chris@0
|
306 }
|
Chris@0
|
307
|
Chris@0
|
308 return $prev;
|
Chris@0
|
309 }
|
Chris@0
|
310
|
Chris@0
|
311 /**
|
Chris@0
|
312 * Sets the PHP error levels for which the stack trace is preserved.
|
Chris@0
|
313 *
|
Chris@0
|
314 * @param int $levels A bit field of E_* constants for traced errors
|
Chris@0
|
315 * @param bool $replace Replace or amend the previous value
|
Chris@0
|
316 *
|
Chris@0
|
317 * @return int The previous value
|
Chris@0
|
318 */
|
Chris@0
|
319 public function traceAt($levels, $replace = false)
|
Chris@0
|
320 {
|
Chris@0
|
321 $prev = $this->tracedErrors;
|
Chris@0
|
322 $this->tracedErrors = (int) $levels;
|
Chris@0
|
323 if (!$replace) {
|
Chris@0
|
324 $this->tracedErrors |= $prev;
|
Chris@0
|
325 }
|
Chris@0
|
326
|
Chris@0
|
327 return $prev;
|
Chris@0
|
328 }
|
Chris@0
|
329
|
Chris@0
|
330 /**
|
Chris@0
|
331 * Sets the error levels where the @-operator is ignored.
|
Chris@0
|
332 *
|
Chris@0
|
333 * @param int $levels A bit field of E_* constants for screamed errors
|
Chris@0
|
334 * @param bool $replace Replace or amend the previous value
|
Chris@0
|
335 *
|
Chris@0
|
336 * @return int The previous value
|
Chris@0
|
337 */
|
Chris@0
|
338 public function screamAt($levels, $replace = false)
|
Chris@0
|
339 {
|
Chris@0
|
340 $prev = $this->screamedErrors;
|
Chris@0
|
341 $this->screamedErrors = (int) $levels;
|
Chris@0
|
342 if (!$replace) {
|
Chris@0
|
343 $this->screamedErrors |= $prev;
|
Chris@0
|
344 }
|
Chris@0
|
345
|
Chris@0
|
346 return $prev;
|
Chris@0
|
347 }
|
Chris@0
|
348
|
Chris@0
|
349 /**
|
Chris@0
|
350 * Re-registers as a PHP error handler if levels changed.
|
Chris@0
|
351 */
|
Chris@0
|
352 private function reRegister($prev)
|
Chris@0
|
353 {
|
Chris@0
|
354 if ($prev !== $this->thrownErrors | $this->loggedErrors) {
|
Chris@0
|
355 $handler = set_error_handler('var_dump');
|
Chris@17
|
356 $handler = \is_array($handler) ? $handler[0] : null;
|
Chris@0
|
357 restore_error_handler();
|
Chris@0
|
358 if ($handler === $this) {
|
Chris@0
|
359 restore_error_handler();
|
Chris@0
|
360 if ($this->isRoot) {
|
Chris@17
|
361 set_error_handler([$this, 'handleError'], $this->thrownErrors | $this->loggedErrors);
|
Chris@0
|
362 } else {
|
Chris@17
|
363 set_error_handler([$this, 'handleError']);
|
Chris@0
|
364 }
|
Chris@0
|
365 }
|
Chris@0
|
366 }
|
Chris@0
|
367 }
|
Chris@0
|
368
|
Chris@0
|
369 /**
|
Chris@0
|
370 * Handles errors by filtering then logging them according to the configured bit fields.
|
Chris@0
|
371 *
|
Chris@0
|
372 * @param int $type One of the E_* constants
|
Chris@0
|
373 * @param string $message
|
Chris@0
|
374 * @param string $file
|
Chris@0
|
375 * @param int $line
|
Chris@0
|
376 *
|
Chris@0
|
377 * @return bool Returns false when no handling happens so that the PHP engine can handle the error itself
|
Chris@0
|
378 *
|
Chris@0
|
379 * @throws \ErrorException When $this->thrownErrors requests so
|
Chris@0
|
380 *
|
Chris@0
|
381 * @internal
|
Chris@0
|
382 */
|
Chris@0
|
383 public function handleError($type, $message, $file, $line)
|
Chris@0
|
384 {
|
Chris@0
|
385 // Level is the current error reporting level to manage silent error.
|
Chris@16
|
386 $level = error_reporting();
|
Chris@16
|
387 $silenced = 0 === ($level & $type);
|
Chris@0
|
388 // Strong errors are not authorized to be silenced.
|
Chris@16
|
389 $level |= E_RECOVERABLE_ERROR | E_USER_ERROR | E_DEPRECATED | E_USER_DEPRECATED;
|
Chris@0
|
390 $log = $this->loggedErrors & $type;
|
Chris@0
|
391 $throw = $this->thrownErrors & $type & $level;
|
Chris@0
|
392 $type &= $level | $this->screamedErrors;
|
Chris@0
|
393
|
Chris@0
|
394 if (!$type || (!$log && !$throw)) {
|
Chris@16
|
395 return !$silenced && $type && $log;
|
Chris@0
|
396 }
|
Chris@0
|
397 $scope = $this->scopedErrors & $type;
|
Chris@0
|
398
|
Chris@17
|
399 if (4 < $numArgs = \func_num_args()) {
|
Chris@17
|
400 $context = $scope ? (func_get_arg(4) ?: []) : [];
|
Chris@0
|
401 $backtrace = 5 < $numArgs ? func_get_arg(5) : null; // defined on HHVM
|
Chris@0
|
402 } else {
|
Chris@17
|
403 $context = [];
|
Chris@0
|
404 $backtrace = null;
|
Chris@0
|
405 }
|
Chris@0
|
406
|
Chris@0
|
407 if (isset($context['GLOBALS']) && $scope) {
|
Chris@0
|
408 $e = $context; // Whatever the signature of the method,
|
Chris@0
|
409 unset($e['GLOBALS'], $context); // $context is always a reference in 5.3
|
Chris@0
|
410 $context = $e;
|
Chris@0
|
411 }
|
Chris@0
|
412
|
Chris@0
|
413 if (null !== $backtrace && $type & E_ERROR) {
|
Chris@0
|
414 // E_ERROR fatal errors are triggered on HHVM when
|
Chris@0
|
415 // hhvm.error_handling.call_user_handler_on_fatals=1
|
Chris@0
|
416 // which is the way to get their backtrace.
|
Chris@0
|
417 $this->handleFatalError(compact('type', 'message', 'file', 'line', 'backtrace'));
|
Chris@0
|
418
|
Chris@0
|
419 return true;
|
Chris@0
|
420 }
|
Chris@0
|
421
|
Chris@0
|
422 $logMessage = $this->levels[$type].': '.$message;
|
Chris@0
|
423
|
Chris@0
|
424 if (null !== self::$toStringException) {
|
Chris@0
|
425 $errorAsException = self::$toStringException;
|
Chris@0
|
426 self::$toStringException = null;
|
Chris@0
|
427 } elseif (!$throw && !($type & $level)) {
|
Chris@12
|
428 if (!isset(self::$silencedErrorCache[$id = $file.':'.$line])) {
|
Chris@17
|
429 $lightTrace = $this->tracedErrors & $type ? $this->cleanTrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3), $type, $file, $line, false) : [];
|
Chris@12
|
430 $errorAsException = new SilencedErrorContext($type, $file, $line, $lightTrace);
|
Chris@12
|
431 } elseif (isset(self::$silencedErrorCache[$id][$message])) {
|
Chris@12
|
432 $lightTrace = null;
|
Chris@12
|
433 $errorAsException = self::$silencedErrorCache[$id][$message];
|
Chris@12
|
434 ++$errorAsException->count;
|
Chris@12
|
435 } else {
|
Chris@17
|
436 $lightTrace = [];
|
Chris@12
|
437 $errorAsException = null;
|
Chris@12
|
438 }
|
Chris@12
|
439
|
Chris@12
|
440 if (100 < ++self::$silencedErrorCount) {
|
Chris@17
|
441 self::$silencedErrorCache = $lightTrace = [];
|
Chris@12
|
442 self::$silencedErrorCount = 1;
|
Chris@12
|
443 }
|
Chris@12
|
444 if ($errorAsException) {
|
Chris@12
|
445 self::$silencedErrorCache[$id][$message] = $errorAsException;
|
Chris@12
|
446 }
|
Chris@12
|
447 if (null === $lightTrace) {
|
Chris@12
|
448 return;
|
Chris@12
|
449 }
|
Chris@0
|
450 } else {
|
Chris@0
|
451 if ($scope) {
|
Chris@0
|
452 $errorAsException = new ContextErrorException($logMessage, 0, $type, $file, $line, $context);
|
Chris@0
|
453 } else {
|
Chris@0
|
454 $errorAsException = new \ErrorException($logMessage, 0, $type, $file, $line);
|
Chris@0
|
455 }
|
Chris@0
|
456
|
Chris@0
|
457 // Clean the trace by removing function arguments and the first frames added by the error handler itself.
|
Chris@0
|
458 if ($throw || $this->tracedErrors & $type) {
|
Chris@0
|
459 $backtrace = $backtrace ?: $errorAsException->getTrace();
|
Chris@12
|
460 $lightTrace = $this->cleanTrace($backtrace, $type, $file, $line, $throw);
|
Chris@0
|
461 $this->traceReflector->setValue($errorAsException, $lightTrace);
|
Chris@0
|
462 } else {
|
Chris@17
|
463 $this->traceReflector->setValue($errorAsException, []);
|
Chris@0
|
464 }
|
Chris@0
|
465 }
|
Chris@0
|
466
|
Chris@0
|
467 if ($throw) {
|
Chris@0
|
468 if (E_USER_ERROR & $type) {
|
Chris@0
|
469 for ($i = 1; isset($backtrace[$i]); ++$i) {
|
Chris@0
|
470 if (isset($backtrace[$i]['function'], $backtrace[$i]['type'], $backtrace[$i - 1]['function'])
|
Chris@0
|
471 && '__toString' === $backtrace[$i]['function']
|
Chris@0
|
472 && '->' === $backtrace[$i]['type']
|
Chris@0
|
473 && !isset($backtrace[$i - 1]['class'])
|
Chris@0
|
474 && ('trigger_error' === $backtrace[$i - 1]['function'] || 'user_error' === $backtrace[$i - 1]['function'])
|
Chris@0
|
475 ) {
|
Chris@0
|
476 // Here, we know trigger_error() has been called from __toString().
|
Chris@0
|
477 // HHVM is fine with throwing from __toString() but PHP triggers a fatal error instead.
|
Chris@0
|
478 // A small convention allows working around the limitation:
|
Chris@0
|
479 // given a caught $e exception in __toString(), quitting the method with
|
Chris@0
|
480 // `return trigger_error($e, E_USER_ERROR);` allows this error handler
|
Chris@0
|
481 // to make $e get through the __toString() barrier.
|
Chris@0
|
482
|
Chris@0
|
483 foreach ($context as $e) {
|
Chris@0
|
484 if (($e instanceof \Exception || $e instanceof \Throwable) && $e->__toString() === $message) {
|
Chris@0
|
485 if (1 === $i) {
|
Chris@0
|
486 // On HHVM
|
Chris@0
|
487 $errorAsException = $e;
|
Chris@0
|
488 break;
|
Chris@0
|
489 }
|
Chris@0
|
490 self::$toStringException = $e;
|
Chris@0
|
491
|
Chris@0
|
492 return true;
|
Chris@0
|
493 }
|
Chris@0
|
494 }
|
Chris@0
|
495
|
Chris@0
|
496 if (1 < $i) {
|
Chris@0
|
497 // On PHP (not on HHVM), display the original error message instead of the default one.
|
Chris@0
|
498 $this->handleException($errorAsException);
|
Chris@0
|
499
|
Chris@0
|
500 // Stop the process by giving back the error to the native handler.
|
Chris@0
|
501 return false;
|
Chris@0
|
502 }
|
Chris@0
|
503 }
|
Chris@0
|
504 }
|
Chris@0
|
505 }
|
Chris@0
|
506
|
Chris@0
|
507 throw $errorAsException;
|
Chris@0
|
508 }
|
Chris@0
|
509
|
Chris@0
|
510 if ($this->isRecursive) {
|
Chris@0
|
511 $log = 0;
|
Chris@0
|
512 } elseif (self::$stackedErrorLevels) {
|
Chris@17
|
513 self::$stackedErrors[] = [
|
Chris@0
|
514 $this->loggers[$type][0],
|
Chris@0
|
515 ($type & $level) ? $this->loggers[$type][1] : LogLevel::DEBUG,
|
Chris@0
|
516 $logMessage,
|
Chris@17
|
517 $errorAsException ? ['exception' => $errorAsException] : [],
|
Chris@17
|
518 ];
|
Chris@0
|
519 } else {
|
Chris@18
|
520 if (!\defined('HHVM_VERSION')) {
|
Chris@18
|
521 $currentErrorHandler = set_error_handler('var_dump');
|
Chris@18
|
522 restore_error_handler();
|
Chris@18
|
523 }
|
Chris@18
|
524
|
Chris@0
|
525 try {
|
Chris@0
|
526 $this->isRecursive = true;
|
Chris@0
|
527 $level = ($type & $level) ? $this->loggers[$type][1] : LogLevel::DEBUG;
|
Chris@17
|
528 $this->loggers[$type][0]->log($level, $logMessage, $errorAsException ? ['exception' => $errorAsException] : []);
|
Chris@0
|
529 } finally {
|
Chris@0
|
530 $this->isRecursive = false;
|
Chris@17
|
531
|
Chris@17
|
532 if (!\defined('HHVM_VERSION')) {
|
Chris@18
|
533 set_error_handler($currentErrorHandler);
|
Chris@17
|
534 }
|
Chris@0
|
535 }
|
Chris@0
|
536 }
|
Chris@0
|
537
|
Chris@16
|
538 return !$silenced && $type && $log;
|
Chris@0
|
539 }
|
Chris@0
|
540
|
Chris@0
|
541 /**
|
Chris@0
|
542 * Handles an exception by logging then forwarding it to another handler.
|
Chris@0
|
543 *
|
Chris@0
|
544 * @param \Exception|\Throwable $exception An exception to handle
|
Chris@0
|
545 * @param array $error An array as returned by error_get_last()
|
Chris@0
|
546 *
|
Chris@0
|
547 * @internal
|
Chris@0
|
548 */
|
Chris@0
|
549 public function handleException($exception, array $error = null)
|
Chris@0
|
550 {
|
Chris@0
|
551 if (null === $error) {
|
Chris@0
|
552 self::$exitCode = 255;
|
Chris@0
|
553 }
|
Chris@0
|
554 if (!$exception instanceof \Exception) {
|
Chris@0
|
555 $exception = new FatalThrowableError($exception);
|
Chris@0
|
556 }
|
Chris@0
|
557 $type = $exception instanceof FatalErrorException ? $exception->getSeverity() : E_ERROR;
|
Chris@12
|
558 $handlerException = null;
|
Chris@0
|
559
|
Chris@0
|
560 if (($this->loggedErrors & $type) || $exception instanceof FatalThrowableError) {
|
Chris@0
|
561 if ($exception instanceof FatalErrorException) {
|
Chris@0
|
562 if ($exception instanceof FatalThrowableError) {
|
Chris@17
|
563 $error = [
|
Chris@0
|
564 'type' => $type,
|
Chris@0
|
565 'message' => $message = $exception->getMessage(),
|
Chris@0
|
566 'file' => $exception->getFile(),
|
Chris@0
|
567 'line' => $exception->getLine(),
|
Chris@17
|
568 ];
|
Chris@0
|
569 } else {
|
Chris@0
|
570 $message = 'Fatal '.$exception->getMessage();
|
Chris@0
|
571 }
|
Chris@0
|
572 } elseif ($exception instanceof \ErrorException) {
|
Chris@0
|
573 $message = 'Uncaught '.$exception->getMessage();
|
Chris@0
|
574 } else {
|
Chris@0
|
575 $message = 'Uncaught Exception: '.$exception->getMessage();
|
Chris@0
|
576 }
|
Chris@0
|
577 }
|
Chris@0
|
578 if ($this->loggedErrors & $type) {
|
Chris@0
|
579 try {
|
Chris@17
|
580 $this->loggers[$type][0]->log($this->loggers[$type][1], $message, ['exception' => $exception]);
|
Chris@0
|
581 } catch (\Exception $handlerException) {
|
Chris@0
|
582 } catch (\Throwable $handlerException) {
|
Chris@0
|
583 }
|
Chris@0
|
584 }
|
Chris@0
|
585 if ($exception instanceof FatalErrorException && !$exception instanceof OutOfMemoryException && $error) {
|
Chris@0
|
586 foreach ($this->getFatalErrorHandlers() as $handler) {
|
Chris@0
|
587 if ($e = $handler->handleError($error, $exception)) {
|
Chris@0
|
588 $exception = $e;
|
Chris@0
|
589 break;
|
Chris@0
|
590 }
|
Chris@0
|
591 }
|
Chris@0
|
592 }
|
Chris@13
|
593 $exceptionHandler = $this->exceptionHandler;
|
Chris@13
|
594 $this->exceptionHandler = null;
|
Chris@0
|
595 try {
|
Chris@13
|
596 if (null !== $exceptionHandler) {
|
Chris@13
|
597 return \call_user_func($exceptionHandler, $exception);
|
Chris@12
|
598 }
|
Chris@12
|
599 $handlerException = $handlerException ?: $exception;
|
Chris@0
|
600 } catch (\Exception $handlerException) {
|
Chris@0
|
601 } catch (\Throwable $handlerException) {
|
Chris@0
|
602 }
|
Chris@12
|
603 if ($exception === $handlerException) {
|
Chris@12
|
604 self::$reservedMemory = null; // Disable the fatal error handler
|
Chris@12
|
605 throw $exception; // Give back $exception to the native handler
|
Chris@0
|
606 }
|
Chris@12
|
607 $this->handleException($handlerException);
|
Chris@0
|
608 }
|
Chris@0
|
609
|
Chris@0
|
610 /**
|
Chris@0
|
611 * Shutdown registered function for handling PHP fatal errors.
|
Chris@0
|
612 *
|
Chris@0
|
613 * @param array $error An array as returned by error_get_last()
|
Chris@0
|
614 *
|
Chris@0
|
615 * @internal
|
Chris@0
|
616 */
|
Chris@0
|
617 public static function handleFatalError(array $error = null)
|
Chris@0
|
618 {
|
Chris@0
|
619 if (null === self::$reservedMemory) {
|
Chris@0
|
620 return;
|
Chris@0
|
621 }
|
Chris@0
|
622
|
Chris@12
|
623 $handler = self::$reservedMemory = null;
|
Chris@17
|
624 $handlers = [];
|
Chris@12
|
625 $previousHandler = null;
|
Chris@12
|
626 $sameHandlerLimit = 10;
|
Chris@0
|
627
|
Chris@17
|
628 while (!\is_array($handler) || !$handler[0] instanceof self) {
|
Chris@12
|
629 $handler = set_exception_handler('var_dump');
|
Chris@12
|
630 restore_exception_handler();
|
Chris@0
|
631
|
Chris@12
|
632 if (!$handler) {
|
Chris@12
|
633 break;
|
Chris@12
|
634 }
|
Chris@12
|
635 restore_exception_handler();
|
Chris@12
|
636
|
Chris@12
|
637 if ($handler !== $previousHandler) {
|
Chris@12
|
638 array_unshift($handlers, $handler);
|
Chris@12
|
639 $previousHandler = $handler;
|
Chris@12
|
640 } elseif (0 === --$sameHandlerLimit) {
|
Chris@12
|
641 $handler = null;
|
Chris@12
|
642 break;
|
Chris@12
|
643 }
|
Chris@12
|
644 }
|
Chris@12
|
645 foreach ($handlers as $h) {
|
Chris@12
|
646 set_exception_handler($h);
|
Chris@12
|
647 }
|
Chris@12
|
648 if (!$handler) {
|
Chris@0
|
649 return;
|
Chris@0
|
650 }
|
Chris@12
|
651 if ($handler !== $h) {
|
Chris@12
|
652 $handler[0]->setExceptionHandler($h);
|
Chris@12
|
653 }
|
Chris@12
|
654 $handler = $handler[0];
|
Chris@17
|
655 $handlers = [];
|
Chris@0
|
656
|
Chris@0
|
657 if ($exit = null === $error) {
|
Chris@0
|
658 $error = error_get_last();
|
Chris@0
|
659 }
|
Chris@0
|
660
|
Chris@0
|
661 try {
|
Chris@0
|
662 while (self::$stackedErrorLevels) {
|
Chris@0
|
663 static::unstackErrors();
|
Chris@0
|
664 }
|
Chris@0
|
665 } catch (\Exception $exception) {
|
Chris@0
|
666 // Handled below
|
Chris@0
|
667 } catch (\Throwable $exception) {
|
Chris@0
|
668 // Handled below
|
Chris@0
|
669 }
|
Chris@0
|
670
|
Chris@0
|
671 if ($error && $error['type'] &= E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR) {
|
Chris@0
|
672 // Let's not throw anymore but keep logging
|
Chris@0
|
673 $handler->throwAt(0, true);
|
Chris@0
|
674 $trace = isset($error['backtrace']) ? $error['backtrace'] : null;
|
Chris@0
|
675
|
Chris@0
|
676 if (0 === strpos($error['message'], 'Allowed memory') || 0 === strpos($error['message'], 'Out of memory')) {
|
Chris@0
|
677 $exception = new OutOfMemoryException($handler->levels[$error['type']].': '.$error['message'], 0, $error['type'], $error['file'], $error['line'], 2, false, $trace);
|
Chris@0
|
678 } else {
|
Chris@0
|
679 $exception = new FatalErrorException($handler->levels[$error['type']].': '.$error['message'], 0, $error['type'], $error['file'], $error['line'], 2, true, $trace);
|
Chris@0
|
680 }
|
Chris@0
|
681 }
|
Chris@0
|
682
|
Chris@0
|
683 try {
|
Chris@0
|
684 if (isset($exception)) {
|
Chris@0
|
685 self::$exitCode = 255;
|
Chris@0
|
686 $handler->handleException($exception, $error);
|
Chris@0
|
687 }
|
Chris@0
|
688 } catch (FatalErrorException $e) {
|
Chris@0
|
689 // Ignore this re-throw
|
Chris@0
|
690 }
|
Chris@0
|
691
|
Chris@0
|
692 if ($exit && self::$exitCode) {
|
Chris@0
|
693 $exitCode = self::$exitCode;
|
Chris@0
|
694 register_shutdown_function('register_shutdown_function', function () use ($exitCode) { exit($exitCode); });
|
Chris@0
|
695 }
|
Chris@0
|
696 }
|
Chris@0
|
697
|
Chris@0
|
698 /**
|
Chris@0
|
699 * Configures the error handler for delayed handling.
|
Chris@0
|
700 * Ensures also that non-catchable fatal errors are never silenced.
|
Chris@0
|
701 *
|
Chris@0
|
702 * As shown by http://bugs.php.net/42098 and http://bugs.php.net/60724
|
Chris@0
|
703 * PHP has a compile stage where it behaves unusually. To workaround it,
|
Chris@0
|
704 * we plug an error handler that only stacks errors for later.
|
Chris@0
|
705 *
|
Chris@0
|
706 * The most important feature of this is to prevent
|
Chris@0
|
707 * autoloading until unstackErrors() is called.
|
Chris@12
|
708 *
|
Chris@12
|
709 * @deprecated since version 3.4, to be removed in 4.0.
|
Chris@0
|
710 */
|
Chris@0
|
711 public static function stackErrors()
|
Chris@0
|
712 {
|
Chris@12
|
713 @trigger_error('Support for stacking errors is deprecated since Symfony 3.4 and will be removed in 4.0.', E_USER_DEPRECATED);
|
Chris@12
|
714
|
Chris@0
|
715 self::$stackedErrorLevels[] = error_reporting(error_reporting() | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR);
|
Chris@0
|
716 }
|
Chris@0
|
717
|
Chris@0
|
718 /**
|
Chris@0
|
719 * Unstacks stacked errors and forwards to the logger.
|
Chris@12
|
720 *
|
Chris@12
|
721 * @deprecated since version 3.4, to be removed in 4.0.
|
Chris@0
|
722 */
|
Chris@0
|
723 public static function unstackErrors()
|
Chris@0
|
724 {
|
Chris@12
|
725 @trigger_error('Support for unstacking errors is deprecated since Symfony 3.4 and will be removed in 4.0.', E_USER_DEPRECATED);
|
Chris@12
|
726
|
Chris@0
|
727 $level = array_pop(self::$stackedErrorLevels);
|
Chris@0
|
728
|
Chris@0
|
729 if (null !== $level) {
|
Chris@0
|
730 $errorReportingLevel = error_reporting($level);
|
Chris@0
|
731 if ($errorReportingLevel !== ($level | E_PARSE | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR)) {
|
Chris@0
|
732 // If the user changed the error level, do not overwrite it
|
Chris@0
|
733 error_reporting($errorReportingLevel);
|
Chris@0
|
734 }
|
Chris@0
|
735 }
|
Chris@0
|
736
|
Chris@0
|
737 if (empty(self::$stackedErrorLevels)) {
|
Chris@0
|
738 $errors = self::$stackedErrors;
|
Chris@17
|
739 self::$stackedErrors = [];
|
Chris@0
|
740
|
Chris@0
|
741 foreach ($errors as $error) {
|
Chris@0
|
742 $error[0]->log($error[1], $error[2], $error[3]);
|
Chris@0
|
743 }
|
Chris@0
|
744 }
|
Chris@0
|
745 }
|
Chris@0
|
746
|
Chris@0
|
747 /**
|
Chris@0
|
748 * Gets the fatal error handlers.
|
Chris@0
|
749 *
|
Chris@0
|
750 * Override this method if you want to define more fatal error handlers.
|
Chris@0
|
751 *
|
Chris@0
|
752 * @return FatalErrorHandlerInterface[] An array of FatalErrorHandlerInterface
|
Chris@0
|
753 */
|
Chris@0
|
754 protected function getFatalErrorHandlers()
|
Chris@0
|
755 {
|
Chris@17
|
756 return [
|
Chris@0
|
757 new UndefinedFunctionFatalErrorHandler(),
|
Chris@0
|
758 new UndefinedMethodFatalErrorHandler(),
|
Chris@0
|
759 new ClassNotFoundFatalErrorHandler(),
|
Chris@17
|
760 ];
|
Chris@0
|
761 }
|
Chris@12
|
762
|
Chris@12
|
763 private function cleanTrace($backtrace, $type, $file, $line, $throw)
|
Chris@12
|
764 {
|
Chris@12
|
765 $lightTrace = $backtrace;
|
Chris@12
|
766
|
Chris@12
|
767 for ($i = 0; isset($backtrace[$i]); ++$i) {
|
Chris@12
|
768 if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
|
Chris@17
|
769 $lightTrace = \array_slice($lightTrace, 1 + $i);
|
Chris@12
|
770 break;
|
Chris@12
|
771 }
|
Chris@12
|
772 }
|
Chris@12
|
773 if (!($throw || $this->scopedErrors & $type)) {
|
Chris@12
|
774 for ($i = 0; isset($lightTrace[$i]); ++$i) {
|
Chris@12
|
775 unset($lightTrace[$i]['args'], $lightTrace[$i]['object']);
|
Chris@12
|
776 }
|
Chris@12
|
777 }
|
Chris@12
|
778
|
Chris@12
|
779 return $lightTrace;
|
Chris@12
|
780 }
|
Chris@0
|
781 }
|