|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FOS\RestBundle\Serializer\Normalizer; |
|
4
|
|
|
|
|
5
|
|
|
use FOS\RestBundle\Util\ExceptionValueMap; |
|
6
|
|
|
use JMS\Serializer\Context; |
|
7
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
|
8
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
|
9
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
|
10
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
|
11
|
|
|
use Symfony\Component\ErrorHandler\Exception\FlattenException; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Christian Flothmann <[email protected]> |
|
16
|
|
|
* |
|
17
|
|
|
* @internal |
|
18
|
|
|
*/ |
|
19
|
|
|
class FlattenExceptionHandler implements SubscribingHandlerInterface |
|
20
|
|
|
{ |
|
21
|
|
|
private $messagesMap; |
|
22
|
|
|
private $debug; |
|
23
|
|
|
private $rfc7807; |
|
24
|
|
|
|
|
25
|
4 |
|
public function __construct(ExceptionValueMap $messagesMap, bool $debug, bool $rfc7807) |
|
26
|
|
|
{ |
|
27
|
4 |
|
$this->messagesMap = $messagesMap; |
|
28
|
4 |
|
$this->debug = $debug; |
|
29
|
4 |
|
$this->rfc7807 = $rfc7807; |
|
30
|
4 |
|
} |
|
31
|
|
|
|
|
32
|
6 |
|
public static function getSubscribingMethods(): array |
|
33
|
|
|
{ |
|
34
|
|
|
return [ |
|
35
|
|
|
[ |
|
36
|
6 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|
37
|
6 |
|
'format' => 'json', |
|
38
|
|
|
'type' => FlattenException::class, |
|
39
|
6 |
|
'method' => 'serializeToJson', |
|
40
|
|
|
], |
|
41
|
|
|
[ |
|
42
|
6 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
|
43
|
6 |
|
'format' => 'xml', |
|
44
|
|
|
'type' => FlattenException::class, |
|
45
|
6 |
|
'method' => 'serializeToXml', |
|
46
|
|
|
], |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public function serializeToJson(JsonSerializationVisitor $visitor, FlattenException $exception, array $type, Context $context) |
|
51
|
|
|
{ |
|
52
|
2 |
|
return $visitor->visitArray($this->convertToArray($exception, $context), $type); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function serializeToXml(XmlSerializationVisitor $visitor, FlattenException $exception, array $type, Context $context) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
2 |
|
$data = $this->convertToArray($exception, $context); |
|
58
|
|
|
|
|
59
|
2 |
|
$document = $visitor->getDocument(); |
|
60
|
|
|
|
|
61
|
2 |
|
if (!$visitor->getCurrentNode()) { |
|
62
|
2 |
|
$visitor->createRoot(null, $this->rfc7807 ? 'response' : 'result'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
foreach ($data as $key => $value) { |
|
66
|
2 |
|
$entryNode = $document->createElement($key); |
|
67
|
2 |
|
$visitor->getCurrentNode()->appendChild($entryNode); |
|
68
|
2 |
|
$visitor->setCurrentNode($entryNode); |
|
69
|
|
|
|
|
70
|
2 |
|
$node = $context->getNavigator()->accept($value); |
|
71
|
2 |
|
if (null !== $node) { |
|
72
|
2 |
|
$visitor->getCurrentNode()->appendChild($node); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
$visitor->revertCurrentNode(); |
|
76
|
|
|
} |
|
77
|
2 |
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
private function convertToArray(FlattenException $exception, Context $context): array |
|
80
|
|
|
{ |
|
81
|
4 |
|
if ($context->hasAttribute('status_code')) { |
|
82
|
|
|
$statusCode = $context->getAttribute('status_code'); |
|
83
|
|
|
} else { |
|
84
|
4 |
|
$statusCode = $exception->getStatusCode(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
4 |
|
$showMessage = $this->messagesMap->resolveFromClassName($exception->getClass()); |
|
88
|
|
|
|
|
89
|
4 |
View Code Duplication |
if ($showMessage || $this->debug) { |
|
90
|
4 |
|
$message = $exception->getMessage(); |
|
91
|
|
|
} else { |
|
92
|
|
|
$message = Response::$statusTexts[$statusCode] ?? 'error'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
4 |
|
if ($this->rfc7807) { |
|
96
|
|
|
return [ |
|
97
|
2 |
|
'type' => $context->hasAttribute('type') ? $context->getAttribute('type') : 'https://tools.ietf.org/html/rfc2616#section-10', |
|
98
|
2 |
|
'title' => $context->hasAttribute('title') ? $context->getAttribute('title') : 'An error occurred', |
|
99
|
2 |
|
'status' => $statusCode, |
|
100
|
2 |
|
'detail' => $message, |
|
101
|
|
|
]; |
|
102
|
|
|
} else { |
|
103
|
|
|
return [ |
|
104
|
2 |
|
'code' => $statusCode, |
|
105
|
2 |
|
'message' => $message, |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.