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