comparison core/lib/Drupal/Core/EventSubscriber/ExceptionJsonSubscriber.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 }