function DefaultExceptionSubscriber::setEventResponse

Same name in other branches
  1. 9 core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber::setEventResponse()
  2. 8.9.x core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber::setEventResponse()
  3. 11.x core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber::setEventResponse()
1 call to DefaultExceptionSubscriber::setEventResponse()
DefaultExceptionSubscriber::onException in core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php
Handles errors for this subscriber.

File

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

Class

DefaultExceptionSubscriber
Serializes exceptions in compliance with the JSON:API specification.

Namespace

Drupal\jsonapi\EventSubscriber

Code

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);
}

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