class ExceptionJsonSubscriber in Drupal 10
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber
- 9 core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber
Default handling for JSON errors.
Hierarchy
- class \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber extends \Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase
Expanded class hierarchy of ExceptionJsonSubscriber
1 file declares its use of ExceptionJsonSubscriber
- ExceptionJsonSubscriberTest.php in core/
tests/ Drupal/ Tests/ Core/ EventSubscriber/ ExceptionJsonSubscriberTest.php
1 string reference to 'ExceptionJsonSubscriber'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses ExceptionJsonSubscriber
File
- core/
lib/ Drupal/ Core/ EventSubscriber/ ExceptionJsonSubscriber.php, line 13
Namespace
Drupal\Core\EventSubscriberView source
class ExceptionJsonSubscriber extends HttpExceptionSubscriberBase {
/**
* {@inheritdoc}
*/
protected function getHandledFormats() {
return [
'json',
'drupal_modal',
'drupal_dialog',
'drupal_ajax',
];
}
/**
* {@inheritdoc}
*/
protected static function getPriority() {
// This will fire after the most common HTML handler, since HTML requests
// are still more common than JSON requests.
return -75;
}
/**
* Handles all 4xx errors for JSON.
*
* @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();
// If the exception is cacheable, generate a cacheable response.
if ($exception instanceof CacheableDependencyInterface) {
$response = new CacheableJsonResponse([
'message' => $event
->getThrowable()
->getMessage(),
], $exception
->getStatusCode(), $exception
->getHeaders());
$response
->addCacheableDependency($exception);
}
else {
$response = new JsonResponse([
'message' => $event
->getThrowable()
->getMessage(),
], $exception
->getStatusCode(), $exception
->getHeaders());
}
$event
->setResponse($response);
}
}