class ExceptionJsonSubscriber in Drupal 9
Same name and namespace in other branches
- 8 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
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExceptionJsonSubscriber:: |
protected | function |
Specifies the request formats this subscriber will respond to. Overrides HttpExceptionSubscriberBase:: |
|
ExceptionJsonSubscriber:: |
protected static | function |
Specifies the priority of all listeners in this class. Overrides HttpExceptionSubscriberBase:: |
|
ExceptionJsonSubscriber:: |
public | function | Handles all 4xx errors for JSON. | |
HttpExceptionSubscriberBase:: |
public static | function | Registers the methods in this class that should be listeners. | |
HttpExceptionSubscriberBase:: |
public | function | Handles errors for this subscriber. | 1 |