From 0c0d852d67b4e4343336ad444214b2f722c6a1e4 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 6 Apr 2023 11:38:09 +0200 Subject: [PATCH] [ErrorHandler] Fix sending Vary header with SerializerErrorRenderer --- .../ErrorRenderer/SerializerErrorRenderer.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/ErrorHandler/ErrorRenderer/SerializerErrorRenderer.php b/src/Symfony/Component/ErrorHandler/ErrorRenderer/SerializerErrorRenderer.php index cec8e4d413dce..4d1e752dc1ffe 100644 --- a/src/Symfony/Component/ErrorHandler/ErrorRenderer/SerializerErrorRenderer.php +++ b/src/Symfony/Component/ErrorHandler/ErrorRenderer/SerializerErrorRenderer.php @@ -55,7 +55,7 @@ public function __construct(SerializerInterface $serializer, $format, ErrorRende */ public function render(\Throwable $exception): FlattenException { - $headers = []; + $headers = ['Vary' => 'Accept']; $debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception); if ($debug) { $headers['X-Debug-Exception'] = rawurlencode($exception->getMessage()); @@ -66,19 +66,17 @@ public function render(\Throwable $exception): FlattenException try { $format = \is_string($this->format) ? $this->format : ($this->format)($flattenException); - $headers = [ - 'Content-Type' => Request::getMimeTypes($format)[0] ?? $format, - 'Vary' => 'Accept', - ]; + $headers['Content-Type'] = Request::getMimeTypes($format)[0] ?? $format; - return $flattenException->setAsString($this->serializer->serialize($flattenException, $format, [ + $flattenException->setAsString($this->serializer->serialize($flattenException, $format, [ 'exception' => $exception, 'debug' => $debug, - ])) - ->setHeaders($flattenException->getHeaders() + $headers); + ])); } catch (NotEncodableValueException $e) { - return $this->fallbackErrorRenderer->render($exception); + $flattenException = $this->fallbackErrorRenderer->render($exception); } + + return $flattenException->setHeaders($flattenException->getHeaders() + $headers); } public static function getPreferredFormat(RequestStack $requestStack): \Closure