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 Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface; |
17
|
|
|
use Symfony\Component\ErrorHandler\Exception\FlattenException; |
18
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
19
|
|
|
use Symfony\Component\Serializer\Exception\NotEncodableValueException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @internal |
23
|
|
|
*/ |
24
|
|
|
final class SerializerErrorRenderer implements ErrorRendererInterface |
25
|
|
|
{ |
26
|
|
|
private $serializer; |
27
|
|
|
private $format; |
28
|
|
|
private $fallbackErrorRenderer; |
29
|
|
|
private $debug; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string|callable(FlattenException) $format |
|
|
|
|
33
|
|
|
* @param string|bool $debug |
34
|
|
|
*/ |
35
|
8 |
|
public function __construct(Serializer $serializer, $format, ErrorRendererInterface $fallbackErrorRenderer = null, $debug = false) |
36
|
|
|
{ |
37
|
8 |
|
if (!is_string($format) && !is_callable($format)) { |
38
|
|
|
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))); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
8 |
|
if (!is_bool($debug) && !is_callable($debug)) { |
42
|
|
|
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))); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
8 |
|
$this->serializer = $serializer; |
46
|
8 |
|
$this->format = $format; |
47
|
8 |
|
$this->fallbackErrorRenderer = $fallbackErrorRenderer; |
48
|
8 |
|
$this->debug = $debug; |
49
|
8 |
|
} |
50
|
|
|
|
51
|
8 |
|
public function render(\Throwable $exception): FlattenException |
52
|
|
|
{ |
53
|
8 |
|
$flattenException = FlattenException::createFromThrowable($exception); |
54
|
|
|
|
55
|
|
|
try { |
56
|
8 |
|
$format = is_callable($this->format) ? ($this->format)($flattenException) : $this->format; |
57
|
|
|
|
58
|
8 |
|
$context = new Context(); |
59
|
8 |
|
$context->setAttribute('exception', $exception); |
60
|
8 |
|
$context->setAttribute('debug', is_callable($this->debug) ? ($this->debug)($exception) : $this->debug); |
61
|
|
|
|
62
|
8 |
|
return $flattenException->setAsString($this->serializer->serialize($flattenException, $format, $context)); |
63
|
1 |
|
} catch (NotEncodableValueException $e) { |
64
|
1 |
|
return $this->fallbackErrorRenderer->render($exception); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @see \Symfony\Component\ErrorHandler\ErrorRenderer\SerializerErrorRenderer::getPreferredFormat |
70
|
|
|
*/ |
71
|
5 |
|
public static function getPreferredFormat(RequestStack $requestStack): \Closure |
72
|
|
|
{ |
73
|
|
|
return static function () use ($requestStack) { |
74
|
5 |
|
if (!$request = $requestStack->getCurrentRequest()) { |
75
|
|
|
throw new NotEncodableValueException(); |
76
|
|
|
} |
77
|
|
|
|
78
|
5 |
|
return $request->getPreferredFormat(); |
79
|
5 |
|
}; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @see \Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer::isDebug |
84
|
|
|
*/ |
85
|
4 |
|
public static function isDebug(RequestStack $requestStack, bool $debug): \Closure |
86
|
|
|
{ |
87
|
|
|
return static function () use ($requestStack, $debug): bool { |
88
|
4 |
|
if (!$request = $requestStack->getCurrentRequest()) { |
89
|
|
|
return $debug; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
return $debug && $request->attributes->getBoolean('showException', true); |
93
|
4 |
|
}; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.