You are here

protected function DefaultExceptionSubscriber::setEventResponse in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber::setEventResponse()
1 call to DefaultExceptionSubscriber::setEventResponse()
DefaultExceptionSubscriber::onException in src/EventSubscriber/DefaultExceptionSubscriber.php
Handles errors for this subscriber.

File

src/EventSubscriber/DefaultExceptionSubscriber.php, line 50

Class

DefaultExceptionSubscriber
Serializes exceptions in compliance with the JSON API specification.

Namespace

Drupal\jsonapi\EventSubscriber

Code

protected function setEventResponse(GetResponseForExceptionEvent $event, $status) {

  /* @var \Symfony\Component\HttpKernel\Exception\HttpException $exception */
  $exception = $event
    ->getException();
  $encoded_content = $this->serializer
    ->serialize($exception, 'api_json', [
    'data_wrapper' => 'errors',
  ]);
  $response = new Response($encoded_content, $status);
  $response->headers
    ->set('Content-Type', 'application/vnd.api+json');
  $event
    ->setResponse($response);
}