ExceptionTestSiteSubscriber.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Drupal\Core\EventSubscriber;
  3. use Drupal\Core\Utility\Error;
  4. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  5. /**
  6. * Custom handling of errors when in a system-under-test.
  7. */
  8. class ExceptionTestSiteSubscriber extends HttpExceptionSubscriberBase {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. protected static function getPriority() {
  13. return 3;
  14. }
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected function getHandledFormats() {
  19. return ['html'];
  20. }
  21. /**
  22. * Checks for special handling of errors inside Simpletest.
  23. *
  24. * @todo The $headers array appears to not actually get used at all in the
  25. * original code. It's quite possible that this entire method is now
  26. * vestigial and can be removed.
  27. *
  28. * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
  29. */
  30. public function on500(GetResponseForExceptionEvent $event) {
  31. $exception = $event->getException();
  32. $error = Error::decodeException($exception);
  33. $headers = [];
  34. // When running inside the testing framework, we relay the errors
  35. // to the tested site by the way of HTTP headers.
  36. if (DRUPAL_TEST_IN_CHILD_SITE && !headers_sent() && (!defined('SIMPLETEST_COLLECT_ERRORS') || SIMPLETEST_COLLECT_ERRORS)) {
  37. // $number does not use drupal_static as it should not be reset
  38. // as it uniquely identifies each PHP error.
  39. static $number = 0;
  40. $assertion = [
  41. $error['@message'],
  42. $error['%type'],
  43. [
  44. 'function' => $error['%function'],
  45. 'file' => $error['%file'],
  46. 'line' => $error['%line'],
  47. ],
  48. ];
  49. $headers['X-Drupal-Assertion-' . $number] = rawurlencode(serialize($assertion));
  50. $number++;
  51. }
  52. }
  53. }