annotate 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
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\EventSubscriber;
Chris@0 4
Chris@0 5 use Symfony\Component\HttpFoundation\JsonResponse;
Chris@0 6 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Default handling for JSON errors.
Chris@0 10 */
Chris@0 11 class ExceptionJsonSubscriber extends HttpExceptionSubscriberBase {
Chris@0 12
Chris@0 13 /**
Chris@0 14 * {@inheritdoc}
Chris@0 15 */
Chris@0 16 protected function getHandledFormats() {
Chris@0 17 return ['json', 'drupal_modal', 'drupal_dialog', 'drupal_ajax'];
Chris@0 18 }
Chris@0 19
Chris@0 20 /**
Chris@0 21 * {@inheritdoc}
Chris@0 22 */
Chris@0 23 protected static function getPriority() {
Chris@0 24 // This will fire after the most common HTML handler, since HTML requests
Chris@0 25 // are still more common than JSON requests.
Chris@0 26 return -75;
Chris@0 27 }
Chris@0 28
Chris@0 29 /**
Chris@0 30 * Handles all 4xx errors for JSON.
Chris@0 31 *
Chris@0 32 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
Chris@0 33 * The event to process.
Chris@0 34 */
Chris@0 35 public function on4xx(GetResponseForExceptionEvent $event) {
Chris@0 36 /** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $exception */
Chris@0 37 $exception = $event->getException();
Chris@0 38 $response = new JsonResponse(['message' => $event->getException()->getMessage()], $exception->getStatusCode(), $exception->getHeaders());
Chris@0 39 $event->setResponse($response);
Chris@0 40 }
Chris@0 41
Chris@0 42 }