FinalExceptionSubscriber.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace Drupal\Core\EventSubscriber;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. use Drupal\Core\Config\ConfigFactoryInterface;
  5. use Drupal\Core\StringTranslation\StringTranslationTrait;
  6. use Drupal\Core\Utility\Error;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  10. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. /**
  13. * Last-chance handler for exceptions: the final exception subscriber.
  14. *
  15. * This handler will catch any exceptions not caught elsewhere and report
  16. * them as an error page.
  17. *
  18. * Each format has its own way of handling exceptions:
  19. * - html: exception.default_html, exception.custom_page_html and
  20. * exception.fast_404_html
  21. * - json: exception.default_json
  22. *
  23. * And when the serialization module is installed, all serialization formats are
  24. * handled by a single exception subscriber:: serialization.exception.default.
  25. *
  26. * This exception subscriber runs after all the above (it has a lower priority),
  27. * which makes it the last-chance exception handler. It always sends a plain
  28. * text response. If it's a displayable error and the error level is configured
  29. * to be verbose, then a helpful backtrace is also printed.
  30. */
  31. class FinalExceptionSubscriber implements EventSubscriberInterface {
  32. use StringTranslationTrait;
  33. /**
  34. * @var string
  35. *
  36. * One of the error level constants defined in bootstrap.inc.
  37. */
  38. protected $errorLevel;
  39. /**
  40. * The config factory.
  41. *
  42. * @var \Drupal\Core\Config\ConfigFactoryInterface
  43. */
  44. protected $configFactory;
  45. /**
  46. * Constructs a new FinalExceptionSubscriber.
  47. *
  48. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  49. * The configuration factory.
  50. */
  51. public function __construct(ConfigFactoryInterface $config_factory) {
  52. $this->configFactory = $config_factory;
  53. }
  54. /**
  55. * Gets the configured error level.
  56. *
  57. * @return string
  58. */
  59. protected function getErrorLevel() {
  60. if (!isset($this->errorLevel)) {
  61. $this->errorLevel = $this->configFactory->get('system.logging')->get('error_level');
  62. }
  63. return $this->errorLevel;
  64. }
  65. /**
  66. * Handles exceptions for this subscriber.
  67. *
  68. * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
  69. * The event to process.
  70. */
  71. public function onException(GetResponseForExceptionEvent $event) {
  72. $exception = $event->getException();
  73. $error = Error::decodeException($exception);
  74. // Display the message if the current error reporting level allows this type
  75. // of message to be displayed, and unconditionally in update.php.
  76. $message = '';
  77. if ($this->isErrorDisplayable($error)) {
  78. // If error type is 'User notice' then treat it as debug information
  79. // instead of an error message.
  80. // @see debug()
  81. if ($error['%type'] == 'User notice') {
  82. $error['%type'] = 'Debug';
  83. }
  84. $error = $this->simplifyFileInError($error);
  85. unset($error['backtrace']);
  86. if (!$this->isErrorLevelVerbose()) {
  87. // Without verbose logging, use a simple message.
  88. // We use \Drupal\Component\Render\FormattableMarkup directly here,
  89. // rather than use t() since we are in the middle of error handling, and
  90. // we don't want t() to cause further errors.
  91. $message = new FormattableMarkup('%type: @message in %function (line %line of %file).', $error);
  92. }
  93. else {
  94. // With verbose logging, we will also include a backtrace.
  95. $backtrace_exception = $exception;
  96. while ($backtrace_exception->getPrevious()) {
  97. $backtrace_exception = $backtrace_exception->getPrevious();
  98. }
  99. $backtrace = $backtrace_exception->getTrace();
  100. // First trace is the error itself, already contained in the message.
  101. // While the second trace is the error source and also contained in the
  102. // message, the message doesn't contain argument values, so we output it
  103. // once more in the backtrace.
  104. array_shift($backtrace);
  105. // Generate a backtrace containing only scalar argument values.
  106. $error['@backtrace'] = Error::formatBacktrace($backtrace);
  107. $message = new FormattableMarkup('%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>', $error);
  108. }
  109. }
  110. $content = $this->t('The website encountered an unexpected error. Please try again later.');
  111. $content .= $message ? '</br></br>' . $message : '';
  112. $response = new Response($content, 500, ['Content-Type' => 'text/plain']);
  113. if ($exception instanceof HttpExceptionInterface) {
  114. $response->setStatusCode($exception->getStatusCode());
  115. $response->headers->add($exception->getHeaders());
  116. }
  117. else {
  118. $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR, '500 Service unavailable (with message)');
  119. }
  120. $event->setResponse($response);
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public static function getSubscribedEvents() {
  126. // Run as the final (very late) KernelEvents::EXCEPTION subscriber.
  127. $events[KernelEvents::EXCEPTION][] = ['onException', -256];
  128. return $events;
  129. }
  130. /**
  131. * Checks whether the error level is verbose or not.
  132. *
  133. * @return bool
  134. */
  135. protected function isErrorLevelVerbose() {
  136. return $this->getErrorLevel() === ERROR_REPORTING_DISPLAY_VERBOSE;
  137. }
  138. /**
  139. * Wrapper for error_displayable().
  140. *
  141. * @param $error
  142. * Optional error to examine for ERROR_REPORTING_DISPLAY_SOME.
  143. *
  144. * @return bool
  145. *
  146. * @see \error_displayable
  147. */
  148. protected function isErrorDisplayable($error) {
  149. return error_displayable($error);
  150. }
  151. /**
  152. * Attempts to reduce error verbosity in the error message's file path.
  153. *
  154. * Attempts to reduce verbosity by removing DRUPAL_ROOT from the file path in
  155. * the message. This does not happen for (false) security.
  156. *
  157. * @param $error
  158. * Optional error to examine for ERROR_REPORTING_DISPLAY_SOME.
  159. *
  160. * @return
  161. * The updated $error.
  162. */
  163. protected function simplifyFileInError($error) {
  164. // Attempt to reduce verbosity by removing DRUPAL_ROOT from the file path
  165. // in the message. This does not happen for (false) security.
  166. $root_length = strlen(DRUPAL_ROOT);
  167. if (substr($error['%file'], 0, $root_length) == DRUPAL_ROOT) {
  168. $error['%file'] = substr($error['%file'], $root_length + 1);
  169. }
  170. return $error;
  171. }
  172. }