Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DefaultExceptionSubscriber.php

function DefaultExceptionSubscriber::on4xx

Handles all 4xx errors for all serialization failures.

Parameters

\Symfony\Component\HttpKernel\Event\ExceptionEvent $event: The event to process.

File

core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php, line 70

Class

DefaultExceptionSubscriber
Handles default error responses in serialization formats.

Namespace

Drupal\serialization\EventSubscriber

Code

public function on4xx(ExceptionEvent $event) {
    
    /** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $exception */
    $exception = $event->getThrowable();
    $request = $event->getRequest();
    $format = $request->getRequestFormat();
    $content = [
        'message' => $exception->getMessage(),
    ];
    $encoded_content = $this->serializer
        ->serialize($content, $format);
    $headers = $exception->getHeaders();
    // Add the MIME type from the request to send back in the header.
    $headers['Content-Type'] = $request->getMimeType($format);
    // If the exception is cacheable, generate a cacheable response.
    if ($exception instanceof CacheableDependencyInterface) {
        $response = new CacheableResponse($encoded_content, $exception->getStatusCode(), $headers);
        $response->addCacheableDependency($exception);
    }
    else {
        $response = new Response($encoded_content, $exception->getStatusCode(), $headers);
    }
    $event->setResponse($response);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal