protected DefaultExceptionSubscriber::onHtml(GetResponseForExceptionEvent $event)
Handles any exception as a generic error page for HTML.
Parameters
\Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event: The event to process.
File
- core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php, line 68
Class
- DefaultExceptionSubscriber
- Last-chance handler for exceptions.
Namespace
Drupal\Core\EventSubscriber
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | protected function onHtml(GetResponseForExceptionEvent $event ) { $exception = $event ->getException(); $error = Error::decodeException( $exception ); // 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 error type is 'User notice' then treat it as debug information // instead of an error message. // @see debug() if ( $error [ '%type' ] == 'User notice' ) { $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); } unset( $error [ 'backtrace' ]); if ( $this ->getErrorLevel() != ERROR_REPORTING_DISPLAY_VERBOSE) { // Without verbose logging, use a simple message. // We call SafeMarkup::format directly here, rather than use t() since // we are in the middle of error handling, and we don't want t() to // cause further errors. $message = SafeMarkup::format( '%type: @message in %function (line %line of %file).' , $error ); } else { // With verbose logging, we will also include a backtrace. $backtrace_exception = $exception ; while ( $backtrace_exception ->getPrevious()) { $backtrace_exception = $backtrace_exception ->getPrevious(); } $backtrace = $backtrace_exception ->getTrace(); // First trace is the error itself, already contained in the message. // While the second trace is the error source and also contained in the // message, the message doesn't contain argument values, so we output it // once more in the backtrace. array_shift ( $backtrace ); // Generate a backtrace containing only scalar argument values. $error [ '@backtrace' ] = Error::formatBacktrace( $backtrace ); $message = SafeMarkup::format( '%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>' , $error ); } } $content = $this ->t( 'The website encountered an unexpected error. Please try again later.' ); $content .= $message ? '</br></br>' . $message : '' ; $response = new Response( $content , 500); if ( $exception instanceof HttpExceptionInterface) { $response ->setStatusCode( $exception ->getStatusCode()); $response ->headers->add( $exception ->getHeaders()); } else { $response ->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR, '500 Service unavailable (with message)' ); } $event ->setResponse( $response ); } |
Please login to continue.