You are here

class ExceptionJsonSubscriber in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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
ExceptionHalJsonSubscriber.php in core/modules/hal/src/EventSubscriber/ExceptionHalJsonSubscriber.php
Contains \Drupal\hal\EventSubscriber\ExceptionHalJsonSubscriber.
1 string reference to 'ExceptionJsonSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
2 services use ExceptionJsonSubscriber
exception.custom_page_json in core/core.services.yml
Drupal\Core\EventSubscriber\ExceptionJsonSubscriber
exception.default_json in core/core.services.yml
Drupal\Core\EventSubscriber\ExceptionJsonSubscriber

File

core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php, line 17
Contains \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber.

Namespace

Drupal\Core\EventSubscriber
View source
class ExceptionJsonSubscriber extends HttpExceptionSubscriberBase {

  /**
   * {@inheritDoc}
   */
  protected function getHandledFormats() {
    return [
      'json',
    ];
  }

  /**
   * {@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 a 403 error for JSON.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on403(GetResponseForExceptionEvent $event) {
    $response = new JsonResponse(array(
      'message' => $event
        ->getException()
        ->getMessage(),
    ), Response::HTTP_FORBIDDEN);
    $event
      ->setResponse($response);
  }

  /**
   * Handles a 404 error for JSON.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on404(GetResponseForExceptionEvent $event) {
    $response = new JsonResponse(array(
      'message' => $event
        ->getException()
        ->getMessage(),
    ), Response::HTTP_NOT_FOUND);
    $event
      ->setResponse($response);
  }

  /**
   * Handles a 405 error for JSON.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on405(GetResponseForExceptionEvent $event) {
    $response = new JsonResponse(array(
      'message' => $event
        ->getException()
        ->getMessage(),
    ), Response::HTTP_METHOD_NOT_ALLOWED);
    $event
      ->setResponse($response);
  }

  /**
   * Handles a 406 error for JSON.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function on406(GetResponseForExceptionEvent $event) {
    $response = new JsonResponse([
      'message' => $event
        ->getException()
        ->getMessage(),
    ], Response::HTTP_NOT_ACCEPTABLE);
    $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 1
ExceptionJsonSubscriber::getPriority protected static function Specifies the priority of all listeners in this class. Overrides HttpExceptionSubscriberBase::getPriority
ExceptionJsonSubscriber::on403 public function Handles a 403 error for JSON.
ExceptionJsonSubscriber::on404 public function Handles a 404 error for JSON.
ExceptionJsonSubscriber::on405 public function Handles a 405 error for JSON.
ExceptionJsonSubscriber::on406 public function Handles a 406 error for JSON.
HttpExceptionSubscriberBase::getSubscribedEvents public static function Registers the methods in this class that should be listeners. Overrides EventSubscriberInterface::getSubscribedEvents
HttpExceptionSubscriberBase::onException public function Handles errors for this subscriber.