diff --git a/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php b/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php index c2bd175..bc1210a 100644 --- a/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php +++ b/core/modules/serialization/src/EventSubscriber/DefaultExceptionSubscriber.php @@ -3,6 +3,7 @@ namespace Drupal\serialization\EventSubscriber; use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase; +use Drupal\Core\EventSubscriber\MainContentViewSubscriber; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; @@ -60,12 +61,20 @@ protected static function getPriority() { * {@inheritdoc} */ public function onException(GetResponseForExceptionEvent $event) { - parent::onException($event); - $format = $event->getRequest()->getRequestFormat(); - $content = ['message' => $event->getException()->getMessage()]; - $encoded_content = $this->serializer->serialize($content, $format); $exception = $event->getException(); - if ($exception instanceof HttpExceptionInterface && (empty($handled_formats) || in_array($format, $handled_formats))) { + + // Make the exception available for example when rendering a block. + $request = $event->getRequest(); + $request->attributes->set('exception', $exception); + + $handled_formats = $this->getHandledFormats(); + + $format = $request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat()); + + if ($exception instanceof HttpExceptionInterface && in_array($format, $handled_formats)) { + $format = $event->getRequest()->getRequestFormat(); + $content = ['message' => $event->getException()->getMessage()]; + $encoded_content = $this->serializer->serialize($content, $format); $response = new Response($encoded_content, $exception->getStatusCode()); $event->setResponse($response); }