You are here

class ExceptionJsonSubscriber in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber

Default handling for JSON errors.

Hierarchy

Expanded class hierarchy of ExceptionJsonSubscriber

1 file declares its use of ExceptionJsonSubscriber
ExceptionJsonSubscriberTest.php in core/tests/Drupal/Tests/Core/EventSubscriber/ExceptionJsonSubscriberTest.php
1 string reference to 'ExceptionJsonSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ExceptionJsonSubscriber
exception.default_json in core/core.services.yml
Drupal\Core\EventSubscriber\ExceptionJsonSubscriber

File

core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php, line 13

Namespace

Drupal\Core\EventSubscriber
View source
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\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on4xx(GetResponseForExceptionEvent $event) {

    /** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $exception */
    $exception = $event
      ->getException();

    // If the exception is cacheable, generate a cacheable response.
    if ($exception instanceof CacheableDependencyInterface) {
      $response = new CacheableJsonResponse([
        'message' => $event
          ->getException()
          ->getMessage(),
      ], $exception
        ->getStatusCode(), $exception
        ->getHeaders());
      $response
        ->addCacheableDependency($exception);
    }
    else {
      $response = new JsonResponse([
        'message' => $event
          ->getException()
          ->getMessage(),
      ], $exception
        ->getStatusCode(), $exception
        ->getHeaders());
    }
    $event
      ->setResponse($response);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExceptionJsonSubscriber::getHandledFormats protected function Specifies the request formats this subscriber will respond to. Overrides HttpExceptionSubscriberBase::getHandledFormats
ExceptionJsonSubscriber::getPriority protected static function Specifies the priority of all listeners in this class. Overrides HttpExceptionSubscriberBase::getPriority
ExceptionJsonSubscriber::on4xx public function Handles all 4xx errors for JSON.
HttpExceptionSubscriberBase::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
HttpExceptionSubscriberBase::onException public function Handles errors for this subscriber.