class DefaultExceptionSubscriber in Drupal 9
Same name in this branch
- 9 core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber
- 9 core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
Same name and namespace in other branches
- 8 core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
Handles default error responses in serialization formats.
Hierarchy
- class \Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
- class \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
Expanded class hierarchy of DefaultExceptionSubscriber
2 files declare their use of DefaultExceptionSubscriber
- DefaultExceptionSubscriber.php in core/
modules/ jsonapi/ src/ EventSubscriber/ DefaultExceptionSubscriber.php - DefaultExceptionSubscriberTest.php in core/
modules/ serialization/ tests/ src/ Unit/ EventSubscriber/ DefaultExceptionSubscriberTest.php
1 string reference to 'DefaultExceptionSubscriber'
- serialization.services.yml in core/
modules/ serialization/ serialization.services.yml - core/modules/serialization/serialization.services.yml
1 service uses DefaultExceptionSubscriber
- serialization.exception.default in core/
modules/ serialization/ serialization.services.yml - Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
File
- core/
modules/ serialization/ src/ EventSubscriber/ DefaultExceptionSubscriber.php, line 15
Namespace
Drupal\serialization\EventSubscriberView source
class DefaultExceptionSubscriber extends HttpExceptionSubscriberBase {
/**
* The serializer.
*
* @var \Symfony\Component\Serializer\Serializer
*/
protected $serializer;
/**
* The available serialization formats.
*
* @var array
*/
protected $serializerFormats = [];
/**
* DefaultExceptionSubscriber constructor.
*
* @param \Symfony\Component\Serializer\SerializerInterface $serializer
* The serializer service.
* @param array $serializer_formats
* The available serialization formats.
*/
public function __construct(SerializerInterface $serializer, array $serializer_formats) {
$this->serializer = $serializer;
$this->serializerFormats = $serializer_formats;
}
/**
* {@inheritdoc}
*/
protected function getHandledFormats() {
return $this->serializerFormats;
}
/**
* {@inheritdoc}
*/
protected static function getPriority() {
// This will fire after the most common HTML handler, since HTML requests
// are still more common than HTTP requests. But it has a lower priority
// than \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber::on4xx(), so
// that this also handles the 'json' format. Then all serialization formats
// (::getHandledFormats()) are handled by this exception subscriber, which
// results in better consistency.
return -70;
}
/**
* Handles all 4xx errors for all serialization failures.
*
* @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
* The event to process.
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultExceptionSubscriber:: |
protected | property | The serializer. | |
DefaultExceptionSubscriber:: |
protected | property | The available serialization formats. | |
DefaultExceptionSubscriber:: |
protected | function |
Specifies the request formats this subscriber will respond to. Overrides HttpExceptionSubscriberBase:: |
1 |
DefaultExceptionSubscriber:: |
protected static | function |
Specifies the priority of all listeners in this class. Overrides HttpExceptionSubscriberBase:: |
1 |
DefaultExceptionSubscriber:: |
public | function | Handles all 4xx errors for all serialization failures. | |
DefaultExceptionSubscriber:: |
public | function | DefaultExceptionSubscriber constructor. | |
HttpExceptionSubscriberBase:: |
public static | function | Registers the methods in this class that should be listeners. | |
HttpExceptionSubscriberBase:: |
public | function | Handles errors for this subscriber. | 1 |