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 $statusCodeMap; |
22
|
|
|
private $messagesMap; |
23
|
|
|
private $debug; |
24
|
|
|
private $rfc7807; |
25
|
|
|
|
26
|
6 |
View Code Duplication |
public function __construct(ExceptionValueMap $statusCodeMap, ExceptionValueMap $messagesMap, bool $debug, bool $rfc7807) |
|
|
|
|
27
|
|
|
{ |
28
|
6 |
|
$this->statusCodeMap = $statusCodeMap; |
29
|
6 |
|
$this->messagesMap = $messagesMap; |
30
|
6 |
|
$this->debug = $debug; |
31
|
6 |
|
$this->rfc7807 = $rfc7807; |
32
|
6 |
|
} |
33
|
|
|
|
34
|
6 |
|
public static function getSubscribingMethods(): array |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
[ |
38
|
6 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
39
|
6 |
|
'format' => 'json', |
40
|
|
|
'type' => FlattenException::class, |
41
|
6 |
|
'method' => 'serializeToJson', |
42
|
|
|
], |
43
|
|
|
[ |
44
|
6 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
45
|
6 |
|
'format' => 'xml', |
46
|
|
|
'type' => FlattenException::class, |
47
|
6 |
|
'method' => 'serializeToXml', |
48
|
|
|
], |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
public function serializeToJson(JsonSerializationVisitor $visitor, FlattenException $exception, array $type, Context $context) |
53
|
|
|
{ |
54
|
4 |
|
if ($this->rfc7807) { |
55
|
2 |
|
$exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+json']); |
56
|
|
|
} |
57
|
|
|
|
58
|
4 |
|
return $visitor->visitArray($this->convertToArray($exception, $context), $type, $context); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
public function serializeToXml(XmlSerializationVisitor $visitor, FlattenException $exception, array $type, Context $context) |
|
|
|
|
62
|
|
|
{ |
63
|
2 |
|
if ($this->rfc7807) { |
64
|
1 |
|
$exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+xml']); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
$rootName = $this->rfc7807 ? 'response' : 'result'; |
68
|
|
|
|
69
|
2 |
|
$data = $this->convertToArray($exception, $context); |
70
|
|
|
|
71
|
2 |
|
if (method_exists($visitor, 'setDefaultRootName')) { |
72
|
|
|
$visitor->setDefaultRootName($rootName); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
$document = $visitor->getDocument(true); |
|
|
|
|
76
|
|
|
|
77
|
2 |
|
if (!$visitor->getCurrentNode()) { |
78
|
2 |
|
$visitor->createRoot(null, $rootName); |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
foreach ($data as $key => $value) { |
82
|
2 |
|
$entryNode = $document->createElement($key); |
83
|
2 |
|
$visitor->getCurrentNode()->appendChild($entryNode); |
84
|
2 |
|
$visitor->setCurrentNode($entryNode); |
85
|
|
|
|
86
|
2 |
|
$node = $context->getNavigator()->accept($value, null, $context); |
|
|
|
|
87
|
2 |
|
if (null !== $node) { |
88
|
2 |
|
$visitor->getCurrentNode()->appendChild($node); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
$visitor->revertCurrentNode(); |
92
|
|
|
} |
93
|
2 |
|
} |
94
|
|
|
|
95
|
6 |
|
private function convertToArray(FlattenException $exception, Context $context): array |
96
|
|
|
{ |
97
|
6 |
View Code Duplication |
if ($context->hasAttribute('status_code')) { |
98
|
|
|
$statusCode = $context->getAttribute('status_code'); |
99
|
6 |
|
} elseif (false === $statusCode = $this->statusCodeMap->resolveFromClassName($exception->getClass())) { |
100
|
4 |
|
$statusCode = $exception->getStatusCode(); |
101
|
|
|
} |
102
|
|
|
|
103
|
6 |
|
$showMessage = $this->messagesMap->resolveFromClassName($exception->getClass()); |
104
|
|
|
|
105
|
6 |
View Code Duplication |
if ($showMessage || $this->debug) { |
106
|
6 |
|
$message = $exception->getMessage(); |
107
|
|
|
} else { |
108
|
|
|
$message = Response::$statusTexts[$statusCode] ?? 'error'; |
109
|
|
|
} |
110
|
|
|
|
111
|
6 |
|
if ($this->rfc7807) { |
112
|
|
|
return [ |
113
|
3 |
|
'type' => $context->hasAttribute('type') ? $context->getAttribute('type') : 'https://tools.ietf.org/html/rfc2616#section-10', |
114
|
3 |
|
'title' => $context->hasAttribute('title') ? $context->getAttribute('title') : 'An error occurred', |
115
|
3 |
|
'status' => $statusCode, |
116
|
3 |
|
'detail' => $message, |
117
|
|
|
]; |
118
|
|
|
} else { |
119
|
|
|
return [ |
120
|
3 |
|
'code' => $statusCode, |
121
|
3 |
|
'message' => $message, |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.