class DefaultExceptionSubscriber in Drupal 8
Same name in this branch
- 8 core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber
- 8 core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
Same name and namespace in other branches
- 9 core/modules/jsonapi/src/EventSubscriber/DefaultExceptionSubscriber.php \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber
Serializes exceptions in compliance with the JSON:API specification.
@internal JSON:API maintains no PHP API. The API is the HTTP API. This class may change at any time and could break any dependencies on it.
Hierarchy
- class \Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
- class \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
- class \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber
- class \Drupal\serialization\EventSubscriber\DefaultExceptionSubscriber
Expanded class hierarchy of DefaultExceptionSubscriber
See also
https://www.drupal.org/project/drupal/issues/3032787
1 string reference to 'DefaultExceptionSubscriber'
- jsonapi.services.yml in core/
modules/ jsonapi/ jsonapi.services.yml - core/modules/jsonapi/jsonapi.services.yml
1 service uses DefaultExceptionSubscriber
File
- core/
modules/ jsonapi/ src/ EventSubscriber/ DefaultExceptionSubscriber.php, line 24
Namespace
Drupal\jsonapi\EventSubscriberView source
class DefaultExceptionSubscriber extends SerializationDefaultExceptionSubscriber {
/**
* {@inheritdoc}
*/
protected static function getPriority() {
return parent::getPriority() + 25;
}
/**
* {@inheritdoc}
*/
protected function getHandledFormats() {
return [
'api_json',
];
}
/**
* {@inheritdoc}
*/
public function onException(GetResponseForExceptionEvent $event) {
if (!$this
->isJsonApiExceptionEvent($event)) {
return;
}
if (($exception = $event
->getException()) && !$exception instanceof HttpException) {
$exception = new HttpException(500, $exception
->getMessage(), $exception);
$event
->setException($exception);
}
$this
->setEventResponse($event, $exception
->getStatusCode());
}
/**
* {@inheritdoc}
*/
protected function setEventResponse(GetResponseForExceptionEvent $event, $status) {
/* @var \Symfony\Component\HttpKernel\Exception\HttpException $exception */
$exception = $event
->getException();
$response = new ResourceResponse(new JsonApiDocumentTopLevel(new ErrorCollection([
$exception,
]), new NullIncludedData(), new LinkCollection([])), $exception
->getStatusCode(), $exception
->getHeaders());
$response
->addCacheableDependency($exception);
$event
->setResponse($response);
}
/**
* Check if the error should be formatted using JSON:API.
*
* The JSON:API format is supported if the format is explicitly set or the
* request is for a known JSON:API route.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $exception_event
* The exception event.
*
* @return bool
* TRUE if it needs to be formatted using JSON:API. FALSE otherwise.
*/
protected function isJsonApiExceptionEvent(GetResponseForExceptionEvent $exception_event) {
$request = $exception_event
->getRequest();
$parameters = $request->attributes
->all();
return $request
->getRequestFormat() === 'api_json' || (bool) Routes::getResourceTypeNameFromParameters($parameters);
}
}
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 DefaultExceptionSubscriber:: |
|
DefaultExceptionSubscriber:: |
protected static | function |
Specifies the priority of all listeners in this class. Overrides DefaultExceptionSubscriber:: |
|
DefaultExceptionSubscriber:: |
protected | function | Check if the error should be formatted using JSON:API. | |
DefaultExceptionSubscriber:: |
public | function | Handles all 4xx errors for all serialization failures. | |
DefaultExceptionSubscriber:: |
public | function |
Handles errors for this subscriber. Overrides HttpExceptionSubscriberBase:: |
|
DefaultExceptionSubscriber:: |
protected | function | ||
DefaultExceptionSubscriber:: |
public | function | DefaultExceptionSubscriber constructor. | |
HttpExceptionSubscriberBase:: |
public static | function | Registers the methods in this class that should be listeners. |