1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\RestBundle\ErrorRenderer; |
13
|
|
|
|
14
|
|
|
use FOS\RestBundle\Context\Context; |
15
|
|
|
use FOS\RestBundle\Serializer\Serializer; |
16
|
|
|
use JMS\Serializer\Exception\UnsupportedFormatException; |
17
|
|
|
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface; |
18
|
|
|
use Symfony\Component\ErrorHandler\Exception\FlattenException; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
21
|
|
|
use Symfony\Component\Serializer\Exception\NotEncodableValueException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @internal |
25
|
|
|
*/ |
26
|
|
|
final class SerializerErrorRenderer implements ErrorRendererInterface |
27
|
|
|
{ |
28
|
|
|
private $serializer; |
29
|
|
|
private $format; |
30
|
|
|
private $fallbackErrorRenderer; |
31
|
|
|
private $debug; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string|callable(FlattenException) $format |
|
|
|
|
35
|
|
|
* @param string|bool $debug |
36
|
|
|
*/ |
37
|
8 |
|
public function __construct(Serializer $serializer, $format, ErrorRendererInterface $fallbackErrorRenderer = null, $debug = false) |
38
|
|
|
{ |
39
|
8 |
|
if (!is_string($format) && !is_callable($format)) { |
40
|
|
|
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be a string or a callable, "%s" given.', __METHOD__, \is_object($format) ? \get_class($format) : \gettype($format))); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
8 |
|
if (!is_bool($debug) && !is_callable($debug)) { |
44
|
|
|
throw new \TypeError(sprintf('Argument 4 passed to "%s()" must be a boolean or a callable, "%s" given.', __METHOD__, \is_object($debug) ? \get_class($debug) : \gettype($debug))); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
8 |
|
$this->serializer = $serializer; |
48
|
8 |
|
$this->format = $format; |
49
|
8 |
|
$this->fallbackErrorRenderer = $fallbackErrorRenderer; |
50
|
8 |
|
$this->debug = $debug; |
51
|
8 |
|
} |
52
|
|
|
|
53
|
8 |
|
public function render(\Throwable $exception): FlattenException |
54
|
|
|
{ |
55
|
8 |
|
$flattenException = FlattenException::createFromThrowable($exception); |
56
|
|
|
|
57
|
|
|
try { |
58
|
8 |
|
$format = is_callable($this->format) ? ($this->format)($flattenException) : $this->format; |
59
|
|
|
|
60
|
8 |
|
$context = new Context(); |
61
|
8 |
|
$context->setAttribute('exception', $exception); |
62
|
8 |
|
$context->setAttribute('debug', is_callable($this->debug) ? ($this->debug)($exception) : $this->debug); |
63
|
|
|
|
64
|
|
|
$headers = [ |
65
|
8 |
|
'Content-Type' => Request::getMimeTypes($format)[0] ?? $format, |
66
|
8 |
|
'Vary' => 'Accept', |
67
|
|
|
]; |
68
|
|
|
|
69
|
8 |
|
return $flattenException->setAsString($this->serializer->serialize($flattenException, $format, $context))->setHeaders($flattenException->getHeaders() + $headers); |
70
|
1 |
|
} catch (NotEncodableValueException | UnsupportedFormatException $e) { |
71
|
1 |
|
return $this->fallbackErrorRenderer->render($exception); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @see \Symfony\Component\ErrorHandler\ErrorRenderer\SerializerErrorRenderer::getPreferredFormat |
77
|
|
|
*/ |
78
|
5 |
|
public static function getPreferredFormat(RequestStack $requestStack): \Closure |
79
|
|
|
{ |
80
|
|
|
return static function () use ($requestStack) { |
81
|
5 |
|
if (!$request = $requestStack->getCurrentRequest()) { |
82
|
|
|
throw class_exists(NotEncodableValueException::class) ? new NotEncodableValueException() : new UnsupportedFormatException(); |
83
|
|
|
} |
84
|
|
|
|
85
|
5 |
|
return $request->getPreferredFormat(); |
86
|
5 |
|
}; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @see \Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer::isDebug |
91
|
|
|
*/ |
92
|
4 |
|
public static function isDebug(RequestStack $requestStack, bool $debug): \Closure |
93
|
|
|
{ |
94
|
|
|
return static function () use ($requestStack, $debug): bool { |
95
|
4 |
|
if (!$request = $requestStack->getCurrentRequest()) { |
96
|
|
|
return $debug; |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
return $debug && $request->attributes->getBoolean('showException', true); |
100
|
4 |
|
}; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.