class DefaultExceptionSubscriber

Same name in this branch
  1. 10 core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
Same name and namespace in other branches
  1. 11.x core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber
  2. 11.x core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
  3. 9 core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber
  4. 9 core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
  5. 8.9.x core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber
  6. 8.9.x core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
  7. main core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber
  8. main core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber

Serializes exceptions in compliance with the JSON:API specification.

@internal JSON:API maintains no PHP API. The API is the HTTP API. This class may change at any time and could break any dependencies on it.

Hierarchy

  • class \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber extends \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber

Expanded class hierarchy of DefaultExceptionSubscriber

See also

https://www.drupal.org/project/drupal/issues/3032787

jsonapi.api.php

1 string reference to 'DefaultExceptionSubscriber'
jsonapi.services.yml in core/modules/jsonapi/jsonapi.services.yml
core/modules/jsonapi/jsonapi.services.yml
1 service uses DefaultExceptionSubscriber
jsonapi.exception_subscriber in core/modules/jsonapi/jsonapi.services.yml
Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber

File

core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php, line 25

Namespace

Drupal\jsonapi\EventSubscriber
View source
class DefaultExceptionSubscriber extends SerializationDefaultExceptionSubscriber {
  
  /**
   * {@inheritdoc}
   */
  protected static function getPriority() {
    return parent::getPriority() + 25;
  }
  
  /**
   * {@inheritdoc}
   */
  protected function getHandledFormats() {
    return [
      'api_json',
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function onException(ExceptionEvent $event) {
    if (!$this->isJsonApiExceptionEvent($event)) {
      return;
    }
    if (($exception = $event->getThrowable()) && !$exception instanceof HttpException) {
      $exception = new HttpException(500, $exception->getMessage(), $exception);
      $event->setThrowable($exception);
    }
    $this->setEventResponse($event, $exception->getStatusCode());
  }
  
  /**
   * {@inheritdoc}
   */
  protected function setEventResponse(ExceptionEvent $event, $status) {
    /** @var \Symfony\Component\HttpKernel\Exception\HttpException $exception */
    $exception = $event->getThrowable();
    $document = new JsonApiDocumentTopLevel(new ErrorCollection([
      $exception,
    ]), new NullIncludedData(), new LinkCollection([]));
    if ($event->getRequest()
      ->isMethodCacheable()) {
      $response = new CacheableResourceResponse($document, $exception->getStatusCode(), $exception->getHeaders());
      $response->addCacheableDependency($exception);
    }
    else {
      $response = new ResourceResponse($document, $exception->getStatusCode(), $exception->getHeaders());
    }
    $event->setResponse($response);
  }
  
  /**
   * Check if the error should be formatted using JSON:API.
   *
   * The JSON:API format is supported if the format is explicitly set or the
   * request is for a known JSON:API route.
   *
   * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $exception_event
   *   The exception event.
   *
   * @return bool
   *   TRUE if it needs to be formatted using JSON:API. FALSE otherwise.
   */
  protected function isJsonApiExceptionEvent(ExceptionEvent $exception_event) {
    $request = $exception_event->getRequest();
    $parameters = $request->attributes
      ->all();
    return $request->getRequestFormat() === 'api_json' || (bool) Routes::getResourceTypeNameFromParameters($parameters);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DefaultExceptionSubscriber::$serializer protected property The serializer.
DefaultExceptionSubscriber::$serializerFormats protected property The available serialization formats.
DefaultExceptionSubscriber::getHandledFormats protected function Overrides DefaultExceptionSubscriber::getHandledFormats
DefaultExceptionSubscriber::getPriority protected static function Overrides DefaultExceptionSubscriber::getPriority
DefaultExceptionSubscriber::isJsonApiExceptionEvent protected function Check if the error should be formatted using JSON:API.
DefaultExceptionSubscriber::on4xx public function Handles all 4xx errors for all serialization failures.
DefaultExceptionSubscriber::onException public function Overrides HttpExceptionSubscriberBase::onException
DefaultExceptionSubscriber::setEventResponse protected function
DefaultExceptionSubscriber::__construct public function DefaultExceptionSubscriber constructor.
HttpExceptionSubscriberBase::getSubscribedEvents public static function Registers the methods in this class that should be listeners. 1

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