annotate core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.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\serialization\EventSubscriber;
Chris@0 4
Chris@0 5 use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase;
Chris@0 6 use Symfony\Component\HttpFoundation\Response;
Chris@0 7 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
Chris@0 8 use Symfony\Component\Serializer\SerializerInterface;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Handles default error responses in serialization formats.
Chris@0 12 */
Chris@0 13 class DefaultExceptionSubscriber extends HttpExceptionSubscriberBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * The serializer.
Chris@0 17 *
Chris@0 18 * @var \Symfony\Component\Serializer\Serializer
Chris@0 19 */
Chris@0 20 protected $serializer;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The available serialization formats.
Chris@0 24 *
Chris@0 25 * @var array
Chris@0 26 */
Chris@0 27 protected $serializerFormats = [];
Chris@0 28
Chris@0 29 /**
Chris@0 30 * DefaultExceptionSubscriber constructor.
Chris@0 31 *
Chris@0 32 * @param \Symfony\Component\Serializer\SerializerInterface $serializer
Chris@0 33 * The serializer service.
Chris@0 34 * @param array $serializer_formats
Chris@0 35 * The available serialization formats.
Chris@0 36 */
Chris@0 37 public function __construct(SerializerInterface $serializer, array $serializer_formats) {
Chris@0 38 $this->serializer = $serializer;
Chris@0 39 $this->serializerFormats = $serializer_formats;
Chris@0 40 }
Chris@0 41
Chris@0 42 /**
Chris@0 43 * {@inheritdoc}
Chris@0 44 */
Chris@0 45 protected function getHandledFormats() {
Chris@0 46 return $this->serializerFormats;
Chris@0 47 }
Chris@0 48
Chris@0 49 /**
Chris@0 50 * {@inheritdoc}
Chris@0 51 */
Chris@0 52 protected static function getPriority() {
Chris@0 53 // This will fire after the most common HTML handler, since HTML requests
Chris@0 54 // are still more common than HTTP requests.
Chris@0 55 return -75;
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Handles all 4xx errors for all serialization failures.
Chris@0 60 *
Chris@0 61 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
Chris@0 62 * The event to process.
Chris@0 63 */
Chris@0 64 public function on4xx(GetResponseForExceptionEvent $event) {
Chris@0 65 /** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $exception */
Chris@0 66 $exception = $event->getException();
Chris@0 67 $request = $event->getRequest();
Chris@0 68
Chris@0 69 $format = $request->getRequestFormat();
Chris@0 70 $content = ['message' => $event->getException()->getMessage()];
Chris@0 71 $encoded_content = $this->serializer->serialize($content, $format);
Chris@0 72 $headers = $exception->getHeaders();
Chris@0 73
Chris@0 74 // Add the MIME type from the request to send back in the header.
Chris@0 75 $headers['Content-Type'] = $request->getMimeType($format);
Chris@0 76
Chris@0 77 $response = new Response($encoded_content, $exception->getStatusCode(), $headers);
Chris@0 78 $event->setResponse($response);
Chris@0 79 }
Chris@0 80
Chris@0 81 }