ErrorHandler.php 25KB

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