annotate core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\serialization\EventSubscriber;
Chris@0 4
Chris@14 5 use Drupal\Core\Cache\CacheableDependencyInterface;
Chris@14 6 use Drupal\Core\Cache\CacheableResponse;
Chris@0 7 use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase;
Chris@0 8 use Symfony\Component\HttpFoundation\Response;
Chris@0 9 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
Chris@0 10 use Symfony\Component\Serializer\SerializerInterface;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Handles default error responses in serialization formats.
Chris@0 14 */
Chris@0 15 class DefaultExceptionSubscriber extends HttpExceptionSubscriberBase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * The serializer.
Chris@0 19 *
Chris@0 20 * @var \Symfony\Component\Serializer\Serializer
Chris@0 21 */
Chris@0 22 protected $serializer;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * The available serialization formats.
Chris@0 26 *
Chris@0 27 * @var array
Chris@0 28 */
Chris@0 29 protected $serializerFormats = [];
Chris@0 30
Chris@0 31 /**
Chris@0 32 * DefaultExceptionSubscriber constructor.
Chris@0 33 *
Chris@0 34 * @param \Symfony\Component\Serializer\SerializerInterface $serializer
Chris@0 35 * The serializer service.
Chris@0 36 * @param array $serializer_formats
Chris@0 37 * The available serialization formats.
Chris@0 38 */
Chris@0 39 public function __construct(SerializerInterface $serializer, array $serializer_formats) {
Chris@0 40 $this->serializer = $serializer;
Chris@0 41 $this->serializerFormats = $serializer_formats;
Chris@0 42 }
Chris@0 43
Chris@0 44 /**
Chris@0 45 * {@inheritdoc}
Chris@0 46 */
Chris@0 47 protected function getHandledFormats() {
Chris@0 48 return $this->serializerFormats;
Chris@0 49 }
Chris@0 50
Chris@0 51 /**
Chris@0 52 * {@inheritdoc}
Chris@0 53 */
Chris@0 54 protected static function getPriority() {
Chris@0 55 // This will fire after the most common HTML handler, since HTML requests
Chris@14 56 // are still more common than HTTP requests. But it has a lower priority
Chris@14 57 // than \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber::on4xx(), so
Chris@14 58 // that this also handles the 'json' format. Then all serialization formats
Chris@14 59 // (::getHandledFormats()) are handled by this exception subscriber, which
Chris@14 60 // results in better consistency.
Chris@14 61 return -70;
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Handles all 4xx errors for all serialization failures.
Chris@0 66 *
Chris@0 67 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
Chris@0 68 * The event to process.
Chris@0 69 */
Chris@0 70 public function on4xx(GetResponseForExceptionEvent $event) {
Chris@0 71 /** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface $exception */
Chris@0 72 $exception = $event->getException();
Chris@0 73 $request = $event->getRequest();
Chris@0 74
Chris@0 75 $format = $request->getRequestFormat();
Chris@14 76 $content = ['message' => $exception->getMessage()];
Chris@0 77 $encoded_content = $this->serializer->serialize($content, $format);
Chris@0 78 $headers = $exception->getHeaders();
Chris@0 79
Chris@0 80 // Add the MIME type from the request to send back in the header.
Chris@0 81 $headers['Content-Type'] = $request->getMimeType($format);
Chris@0 82
Chris@14 83 // If the exception is cacheable, generate a cacheable response.
Chris@14 84 if ($exception instanceof CacheableDependencyInterface) {
Chris@14 85 $response = new CacheableResponse($encoded_content, $exception->getStatusCode(), $headers);
Chris@14 86 $response->addCacheableDependency($exception);
Chris@14 87 }
Chris@14 88 else {
Chris@14 89 $response = new Response($encoded_content, $exception->getStatusCode(), $headers);
Chris@14 90 }
Chris@14 91
Chris@0 92 $event->setResponse($response);
Chris@0 93 }
Chris@0 94
Chris@0 95 }