6ccb743356de49dc3c874a7e1ca707ae313a4f6b
[yaffs-website] / web / core / lib / Drupal / Core / EventSubscriber / ExceptionJsonSubscriber.php
1 <?php
2
3 namespace Drupal\Core\EventSubscriber;
4
5 use Symfony\Component\HttpFoundation\JsonResponse;
6 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
7
8 /**
9  * Default handling for JSON errors.
10  */
11 class ExceptionJsonSubscriber extends HttpExceptionSubscriberBase {
12
13   /**
14    * {@inheritdoc}
15    */
16   protected function getHandledFormats() {
17     return ['json', 'drupal_modal', 'drupal_dialog', 'drupal_ajax'];
18   }
19
20   /**
21    * {@inheritdoc}
22    */
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.
26     return -75;
27   }
28
29   /**
30    * Handles all 4xx errors for JSON.
31    *
32    * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
33    *   The event to process.
34    */
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);
40   }
41
42 }