vendor/symfony/http-kernel/EventListener/ErrorListener.php line 52

Open in your IDE?
  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\HttpKernel\EventListener;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException;
  13. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  17. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  18. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  19. use Symfony\Component\HttpKernel\Exception\HttpException;
  20. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  21. use Symfony\Component\HttpKernel\HttpKernelInterface;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  24. /**
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. */
  27. class ErrorListener implements EventSubscriberInterface
  28. {
  29. protected $controller;
  30. protected $logger;
  31. protected $debug;
  32. /**
  33. * @var array<class-string, array{log_level: string|null, status_code: int<100,599>|null}>
  34. */
  35. protected $exceptionsMapping;
  36. /**
  37. * @param array<class-string, array{log_level: string|null, status_code: int<100,599>|null}> $exceptionsMapping
  38. */
  39. public function __construct($controller, ?LoggerInterface $logger = null, bool $debug = false, array $exceptionsMapping = [])
  40. {
  41. $this->controller = $controller;
  42. $this->logger = $logger;
  43. $this->debug = $debug;
  44. $this->exceptionsMapping = $exceptionsMapping;
  45. }
  46. public function logKernelException(ExceptionEvent $event)
  47. {
  48. $throwable = $event->getThrowable();
  49. $logLevel = null;
  50. foreach ($this->exceptionsMapping as $class => $config) {
  51. if ($throwable instanceof $class && $config['log_level']) {
  52. $logLevel = $config['log_level'];
  53. break;
  54. }
  55. }
  56. foreach ($this->exceptionsMapping as $class => $config) {
  57. if (!$throwable instanceof $class || !$config['status_code']) {
  58. continue;
  59. }
  60. if (!$throwable instanceof HttpExceptionInterface || $throwable->getStatusCode() !== $config['status_code']) {
  61. $headers = $throwable instanceof HttpExceptionInterface ? $throwable->getHeaders() : [];
  62. $throwable = new HttpException($config['status_code'], $throwable->getMessage(), $throwable, $headers);
  63. $event->setThrowable($throwable);
  64. }
  65. break;
  66. }
  67. $e = FlattenException::createFromThrowable($throwable);
  68. $this->logException($throwable, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()), $logLevel);
  69. }
  70. public function onKernelException(ExceptionEvent $event)
  71. {
  72. if (null === $this->controller) {
  73. return;
  74. }
  75. $throwable = $event->getThrowable();
  76. $request = $this->duplicateRequest($throwable, $event->getRequest());
  77. try {
  78. $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false);
  79. } catch (\Exception $e) {
  80. $f = FlattenException::createFromThrowable($e);
  81. $this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', $f->getClass(), $f->getMessage(), $e->getFile(), $e->getLine()));
  82. $prev = $e;
  83. do {
  84. if ($throwable === $wrapper = $prev) {
  85. throw $e;
  86. }
  87. } while ($prev = $wrapper->getPrevious());
  88. $prev = new \ReflectionProperty($wrapper instanceof \Exception ? \Exception::class : \Error::class, 'previous');
  89. $prev->setAccessible(true);
  90. $prev->setValue($wrapper, $throwable);
  91. throw $e;
  92. }
  93. $event->setResponse($response);
  94. if ($this->debug) {
  95. $event->getRequest()->attributes->set('_remove_csp_headers', true);
  96. }
  97. }
  98. public function removeCspHeader(ResponseEvent $event): void
  99. {
  100. if ($this->debug && $event->getRequest()->attributes->get('_remove_csp_headers', false)) {
  101. $event->getResponse()->headers->remove('Content-Security-Policy');
  102. }
  103. }
  104. public function onControllerArguments(ControllerArgumentsEvent $event)
  105. {
  106. $e = $event->getRequest()->attributes->get('exception');
  107. if (!$e instanceof \Throwable || false === $k = array_search($e, $event->getArguments(), true)) {
  108. return;
  109. }
  110. $r = new \ReflectionFunction(\Closure::fromCallable($event->getController()));
  111. $r = $r->getParameters()[$k] ?? null;
  112. if ($r && (!($r = $r->getType()) instanceof \ReflectionNamedType || \in_array($r->getName(), [FlattenException::class, LegacyFlattenException::class], true))) {
  113. $arguments = $event->getArguments();
  114. $arguments[$k] = FlattenException::createFromThrowable($e);
  115. $event->setArguments($arguments);
  116. }
  117. }
  118. public static function getSubscribedEvents(): array
  119. {
  120. return [
  121. KernelEvents::CONTROLLER_ARGUMENTS => 'onControllerArguments',
  122. KernelEvents::EXCEPTION => [
  123. ['logKernelException', 0],
  124. ['onKernelException', -128],
  125. ],
  126. KernelEvents::RESPONSE => ['removeCspHeader', -128],
  127. ];
  128. }
  129. /**
  130. * Logs an exception.
  131. */
  132. protected function logException(\Throwable $exception, string $message, ?string $logLevel = null): void
  133. {
  134. if (null !== $this->logger) {
  135. if (null !== $logLevel) {
  136. $this->logger->log($logLevel, $message, ['exception' => $exception]);
  137. } elseif (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
  138. $this->logger->critical($message, ['exception' => $exception]);
  139. } else {
  140. $this->logger->error($message, ['exception' => $exception]);
  141. }
  142. }
  143. }
  144. /**
  145. * Clones the request for the exception.
  146. */
  147. protected function duplicateRequest(\Throwable $exception, Request $request): Request
  148. {
  149. $attributes = [
  150. '_controller' => $this->controller,
  151. 'exception' => $exception,
  152. 'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
  153. ];
  154. $request = $request->duplicate(null, null, $attributes);
  155. $request->setMethod('GET');
  156. return $request;
  157. }
  158. }