12
12
namespace Symfony \Component \ErrorHandler \ErrorRenderer ;
13
13
14
14
use Symfony \Component \ErrorHandler \Exception \FlattenException ;
15
+ use Symfony \Component \HttpFoundation \Request ;
15
16
use Symfony \Component \HttpFoundation \RequestStack ;
16
17
use Symfony \Component \Serializer \Exception \NotEncodableValueException ;
17
18
use Symfony \Component \Serializer \SerializerInterface ;
@@ -30,6 +31,7 @@ class SerializerErrorRenderer implements ErrorRendererInterface
30
31
31
32
/**
32
33
* @param string|callable(FlattenException) $format The format as a string or a callable that should return it
34
+ * formats not supported by Request::getMimeTypes() should be given as mime types
33
35
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
34
36
*/
35
37
public function __construct (SerializerInterface $ serializer , $ format , ErrorRendererInterface $ fallbackErrorRenderer = null , $ debug = false )
@@ -57,11 +59,16 @@ public function render(\Throwable $exception): FlattenException
57
59
58
60
try {
59
61
$ format = \is_string ($ this ->format ) ? $ this ->format : ($ this ->format )($ flattenException );
62
+ $ headers = [
63
+ 'Content-Type ' => Request::getMimeTypes ($ format )[0 ] ?? $ format ,
64
+ 'Vary ' => 'Accept ' ,
65
+ ];
60
66
61
67
return $ flattenException ->setAsString ($ this ->serializer ->serialize ($ flattenException , $ format , [
62
68
'exception ' => $ exception ,
63
69
'debug ' => \is_bool ($ this ->debug ) ? $ this ->debug : ($ this ->debug )($ exception ),
64
- ]));
70
+ ]))
71
+ ->setHeaders ($ flattenException ->getHeaders () + $ headers );
65
72
} catch (NotEncodableValueException $ e ) {
66
73
return $ this ->fallbackErrorRenderer ->render ($ exception );
67
74
}
0 commit comments