Same filename in this branch
- 10 core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php
- 10 core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php
Same filename and directory in other branches
- 8.9.x core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php
- 9 core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php
Namespace
Drupal\serialization\EventSubscriber
File
core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php
View source
<?php
namespace Drupal\serialization\EventSubscriber;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\Serializer\SerializerInterface;
class DefaultExceptionSubscriber extends HttpExceptionSubscriberBase {
protected $serializer;
protected $serializerFormats = [];
public function __construct(SerializerInterface $serializer, array $serializer_formats) {
$this->serializer = $serializer;
$this->serializerFormats = $serializer_formats;
}
protected function getHandledFormats() {
return $this->serializerFormats;
}
protected static function getPriority() {
return -70;
}
public function on4xx(ExceptionEvent $event) {
$exception = $event
->getThrowable();
$request = $event
->getRequest();
$format = $request
->getRequestFormat();
$content = [
'message' => $exception
->getMessage(),
];
$encoded_content = $this->serializer
->serialize($content, $format);
$headers = $exception
->getHeaders();
$headers['Content-Type'] = $request
->getMimeType($format);
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);
}
}
Classes