You are here

protected function DefaultExceptionSubscriber::onJson in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\Core\EventSubscriber\DefaultExceptionSubscriber::onJson()

Handles any exception as a generic error page for JSON.

@todo This should probably check the error reporting level.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event: The event to process.

File

core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php, line 148
Contains \Drupal\Core\EventSubscriber\DefaultExceptionSubscriber.

Class

DefaultExceptionSubscriber
Last-chance handler for exceptions.

Namespace

Drupal\Core\EventSubscriber

Code

protected function onJson(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,
  $data = NULL;
  if (error_displayable($error) && ($message = $exception
    ->getMessage())) {
    $data = [
      'message' => sprintf('A fatal error occurred: %s', $message),
    ];
  }
  $response = new JsonResponse($data, Response::HTTP_INTERNAL_SERVER_ERROR);
  if ($exception instanceof HttpExceptionInterface) {
    $response
      ->setStatusCode($exception
      ->getStatusCode());
    $response->headers
      ->add($exception
      ->getHeaders());
  }
  $event
    ->setResponse($response);
}