class ExceptionJsonSubscriber
Same name in other branches
- 9 core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber
- 8.9.x core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber
- 11.x core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber
Default handling for JSON errors.
Hierarchy
- class \Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
- 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);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
ExceptionJsonSubscriber::getHandledFormats | protected | function | Overrides HttpExceptionSubscriberBase::getHandledFormats | ||
ExceptionJsonSubscriber::getPriority | protected static | function | Overrides HttpExceptionSubscriberBase::getPriority | ||
ExceptionJsonSubscriber::on4xx | public | function | Handles all 4xx errors for JSON. | ||
HttpExceptionSubscriberBase::getSubscribedEvents | public static | function | Registers the methods in this class that should be listeners. | 1 | |
HttpExceptionSubscriberBase::onException | public | function | Handles errors for this subscriber. | 1 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.