3 namespace Drupal\Core\EventSubscriber;
5 use Symfony\Component\HttpFoundation\JsonResponse;
6 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
9 * Default handling for JSON errors.
11 class ExceptionJsonSubscriber extends HttpExceptionSubscriberBase {
16 protected function getHandledFormats() {
17 return ['json', 'drupal_modal', 'drupal_dialog', 'drupal_ajax'];
23 protected static function getPriority() {
24 // This will fire after the most common HTML handler, since HTML requests
25 // are still more common than JSON requests.
30 * Handles all 4xx errors for JSON.
32 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
33 * The event to process.
35 public function on4xx(GetResponseForExceptionEvent $event) {
36 /** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $exception */
37 $exception = $event->getException();
38 $response = new JsonResponse(['message' => $event->getException()->getMessage()], $exception->getStatusCode(), $exception->getHeaders());
39 $event->setResponse($response);