.../EventSubscriber/DefaultExceptionSubscriber.php | 55 ++++++++++++++++++---- .../DefaultExceptionSubscriberTest.php | 21 ++++++++- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php index fdc2385..4d3bffc 100644 --- a/core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php @@ -70,7 +70,7 @@ public function onException(GetResponseForExceptionEvent $event) { // Display the message if the current error reporting level allows this type // of message to be displayed, and unconditionally in update.php. $message = ''; - if (error_displayable($error)) { + if ($this->isErrorDisplayable($error)) { // If error type is 'User notice' then treat it as debug information // instead of an error message. // @see debug() @@ -78,16 +78,11 @@ public function onException(GetResponseForExceptionEvent $event) { $error['%type'] = 'Debug'; } - // Attempt to reduce verbosity by removing DRUPAL_ROOT from the file path - // in the message. This does not happen for (false) security. - $root_length = strlen(DRUPAL_ROOT); - if (substr($error['%file'], 0, $root_length) == DRUPAL_ROOT) { - $error['%file'] = substr($error['%file'], $root_length + 1); - } + $error = $this->simplifyFileInError($error); unset($error['backtrace']); - if ($this->getErrorLevel() != ERROR_REPORTING_DISPLAY_VERBOSE) { + if (!$this->isErrorLevelVerbose()) { // Without verbose logging, use a simple message. // We call SafeMarkup::format directly here, rather than use t() since @@ -138,4 +133,48 @@ public static function getSubscribedEvents() { return $events; } + /** + * Checks whether the error level is verbose or not. + * + * @return bool + */ + protected function isErrorLevelVerbose() { + return $this->getErrorLevel() != ERROR_REPORTING_DISPLAY_VERBOSE; + } + + /** + * Wrapper for error_displayable(). + * + * @param $error + * Optional error to examine for ERROR_REPORTING_DISPLAY_SOME. + * + * @return bool + * + * @see \error_displayable + */ + protected function isErrorDisplayable($error) { + return error_displayable($error); + } + + /** + * Attempts to reduce error verbosity in the error message's file path. + * + * Attempts to reduce verbosity by removing DRUPAL_ROOT from the file path in + * the message. This does not happen for (false) security. + * + * @param $error + * Optional error to examine for ERROR_REPORTING_DISPLAY_SOME. + * + * @return + * The updated $error. + */ + protected function simplifyFileInError($error) { + // Attempt to reduce verbosity by removing DRUPAL_ROOT from the file path + // in the message. This does not happen for (false) security. + $root_length = strlen(DRUPAL_ROOT); + if (substr($error['%file'], 0, $root_length) == DRUPAL_ROOT) { + $error['%file'] = substr($error['%file'], $root_length + 1); + } + } + } diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/DefaultExceptionSubscriberTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/DefaultExceptionSubscriberTest.php index 279d830..a0a48a1 100644 --- a/core/tests/Drupal/Tests/Core/EventSubscriber/DefaultExceptionSubscriberTest.php +++ b/core/tests/Drupal/Tests/Core/EventSubscriber/DefaultExceptionSubscriberTest.php @@ -26,12 +26,13 @@ public function testOnExceptionWithUnknownFormat() { $request = Request::create('/test?_format=bananas'); $e = new MethodNotAllowedHttpException(['POST', 'PUT'], 'test message'); $event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e); - $subscriber = new DefaultExceptionSubscriber($config_factory); + $subscriber = new TestDefaultExceptionSubscriber($config_factory); + $subscriber->setStringTranslation($this->getStringTranslationStub()); $subscriber->onException($event); $response = $event->getResponse(); $this->assertInstanceOf(Response::class, $response); - $this->assertEquals('test message', $response->getContent()); + $this->stringStartsWith('The website encountered an unexpected error. Please try again later.

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: test message in ', $response->getContent()); $this->assertEquals(405, $response->getStatusCode()); $this->assertEquals('POST, PUT', $response->headers->get('Allow')); // Also check that that text/plain content type was added. @@ -39,3 +40,19 @@ public function testOnExceptionWithUnknownFormat() { } } + +class TestDefaultExceptionSubscriber extends DefaultExceptionSubscriber { + + protected function isErrorDisplayable($error) { + return TRUE; + } + + protected function simplifyFileInError($error) { + return $error; + } + + protected function isErrorLevelVerbose() { + return TRUE; + } + +} \ No newline at end of file