| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\ErrorHandler\ErrorRenderer;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\ErrorHandler\Exception\FlattenException;
|
|
|
15 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
16 |
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
17 |
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
|
|
|
18 |
use Symfony\Component\Serializer\SerializerInterface;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Formats an exception using Serializer for rendering.
|
|
|
22 |
*
|
|
|
23 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
24 |
*/
|
|
|
25 |
class SerializerErrorRenderer implements ErrorRendererInterface
|
|
|
26 |
{
|
|
|
27 |
private SerializerInterface $serializer;
|
|
|
28 |
private string|\Closure $format;
|
|
|
29 |
private ErrorRendererInterface $fallbackErrorRenderer;
|
|
|
30 |
private bool|\Closure $debug;
|
|
|
31 |
|
|
|
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
|
|
|
35 |
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
|
|
|
36 |
*/
|
|
|
37 |
public function __construct(SerializerInterface $serializer, string|callable $format, ErrorRendererInterface $fallbackErrorRenderer = null, bool|callable $debug = false)
|
|
|
38 |
{
|
|
|
39 |
$this->serializer = $serializer;
|
|
|
40 |
$this->format = \is_string($format) ? $format : $format(...);
|
|
|
41 |
$this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
|
|
|
42 |
$this->debug = \is_bool($debug) ? $debug : $debug(...);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public function render(\Throwable $exception): FlattenException
|
|
|
46 |
{
|
|
|
47 |
$headers = [];
|
|
|
48 |
$debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
|
|
|
49 |
if ($debug) {
|
|
|
50 |
$headers['X-Debug-Exception'] = rawurlencode($exception->getMessage());
|
|
|
51 |
$headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()).':'.$exception->getLine();
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
$flattenException = FlattenException::createFromThrowable($exception, null, $headers);
|
|
|
55 |
|
|
|
56 |
try {
|
|
|
57 |
$format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
|
|
|
58 |
$headers = [
|
|
|
59 |
'Content-Type' => Request::getMimeTypes($format)[0] ?? $format,
|
|
|
60 |
'Vary' => 'Accept',
|
|
|
61 |
];
|
|
|
62 |
|
|
|
63 |
return $flattenException->setAsString($this->serializer->serialize($flattenException, $format, [
|
|
|
64 |
'exception' => $exception,
|
|
|
65 |
'debug' => $debug,
|
|
|
66 |
]))
|
|
|
67 |
->setHeaders($flattenException->getHeaders() + $headers);
|
|
|
68 |
} catch (NotEncodableValueException) {
|
|
|
69 |
return $this->fallbackErrorRenderer->render($exception);
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
public static function getPreferredFormat(RequestStack $requestStack): \Closure
|
|
|
74 |
{
|
|
|
75 |
return static function () use ($requestStack) {
|
|
|
76 |
if (!$request = $requestStack->getCurrentRequest()) {
|
|
|
77 |
throw new NotEncodableValueException();
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
return $request->getPreferredFormat();
|
|
|
81 |
};
|
|
|
82 |
}
|
|
|
83 |
}
|