ExceptionJsonSubscriber.php

Same filename and directory in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php
  2. 8.9.x core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php
  3. 10 core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php

Namespace

Drupal\Core\EventSubscriber

File

core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php

View source
<?php

namespace Drupal\Core\EventSubscriber;

use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableJsonResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;

/**
 * Default handling for JSON errors.
 */
class ExceptionJsonSubscriber extends HttpExceptionSubscriberBase {
  
  /**
   * {@inheritdoc}
   */
  protected function getHandledFormats() {
    return [
      'json',
      'drupal_modal',
      'drupal_dialog',
      'drupal_ajax',
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  protected static function getPriority() {
    // This will fire after the most common HTML handler, since HTML requests
    // are still more common than JSON requests.
    return -75;
  }
  
  /**
   * Handles all 4xx errors for JSON.
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
   *   The event to process.
   */
  public function on4xx(ExceptionEvent $event) {
    /** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $exception */
    $exception = $event->getThrowable();
    // If the exception is cacheable, generate a cacheable response.
    if ($exception instanceof CacheableDependencyInterface) {
      $response = new CacheableJsonResponse([
        'message' => $event->getThrowable()
          ->getMessage(),
      ], $exception->getStatusCode(), $exception->getHeaders());
      $response->addCacheableDependency($exception);
    }
    else {
      $response = new JsonResponse([
        'message' => $event->getThrowable()
          ->getMessage(),
      ], $exception->getStatusCode(), $exception->getHeaders());
    }
    $event->setResponse($response);
  }

}

Classes

Title Deprecated Summary
ExceptionJsonSubscriber Default handling for JSON errors.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.