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\Serializer\Normalizer; |
13
|
|
|
|
14
|
1 |
|
@trigger_error(sprintf('The %s\ExceptionHandler class is deprecated since FOSRestBundle 2.8.', __NAMESPACE__), E_USER_DEPRECATED); |
|
|
|
|
15
|
|
|
|
16
|
|
|
use JMS\Serializer\Context; |
17
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
18
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
19
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
20
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @deprecated since 2.8 |
24
|
|
|
*/ |
25
|
|
|
class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface |
26
|
|
|
{ |
27
|
2 |
|
public static function getSubscribingMethods() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
[ |
31
|
2 |
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
32
|
|
|
'format' => 'json', |
33
|
|
|
'type' => \Error::class, |
34
|
|
|
'method' => 'serializeErrorToJson', |
35
|
|
|
], |
36
|
|
|
[ |
37
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
38
|
|
|
'format' => 'json', |
39
|
|
|
'type' => \Exception::class, |
40
|
|
|
'method' => 'serializeToJson', |
41
|
|
|
], |
42
|
|
|
[ |
43
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
44
|
|
|
'format' => 'xml', |
45
|
|
|
'type' => \Error::class, |
46
|
|
|
'method' => 'serializeErrorToXml', |
47
|
|
|
], |
48
|
|
|
[ |
49
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
50
|
|
|
'format' => 'xml', |
51
|
|
|
'type' => \Exception::class, |
52
|
|
|
'method' => 'serializeToXml', |
53
|
|
|
], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
public function serializeToJson( |
58
|
|
|
JsonSerializationVisitor $visitor, |
59
|
|
|
\Exception $exception, |
60
|
|
|
array $type, |
61
|
|
|
Context $context |
62
|
|
|
) { |
63
|
2 |
|
$data = $this->convertToArray($exception, $context); |
64
|
|
|
|
65
|
2 |
|
return $visitor->visitArray($data, $type, $context); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function serializeErrorToJson( |
69
|
|
|
JsonSerializationVisitor $visitor, |
70
|
|
|
\Throwable $exception, |
71
|
|
|
array $type, |
72
|
|
|
Context $context |
73
|
|
|
) { |
74
|
|
|
$data = $this->convertThrowableToArray($exception, $context); |
75
|
|
|
|
76
|
|
|
return $visitor->visitArray($data, $type, $context); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
View Code Duplication |
public function serializeToXml( |
|
|
|
|
80
|
|
|
XmlSerializationVisitor $visitor, |
81
|
|
|
\Exception $exception, |
82
|
|
|
array $type, |
|
|
|
|
83
|
|
|
Context $context |
84
|
|
|
) { |
85
|
1 |
|
$data = $this->convertToArray($exception, $context); |
86
|
|
|
|
87
|
1 |
|
$document = $visitor->getDocument(true); |
|
|
|
|
88
|
|
|
|
89
|
1 |
|
if (!$visitor->getCurrentNode()) { |
90
|
1 |
|
$visitor->createRoot(); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
foreach ($data as $key => $value) { |
94
|
1 |
|
$entryNode = $document->createElement($key); |
95
|
1 |
|
$visitor->getCurrentNode()->appendChild($entryNode); |
96
|
1 |
|
$visitor->setCurrentNode($entryNode); |
97
|
|
|
|
98
|
1 |
|
$node = $context->getNavigator()->accept($value, null, $context); |
|
|
|
|
99
|
1 |
|
if (null !== $node) { |
100
|
1 |
|
$visitor->getCurrentNode()->appendChild($node); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$visitor->revertCurrentNode(); |
104
|
|
|
} |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
public function serializeErrorToXml( |
|
|
|
|
108
|
|
|
XmlSerializationVisitor $visitor, |
109
|
|
|
\Throwable $exception, |
110
|
|
|
array $type, |
|
|
|
|
111
|
|
|
Context $context |
112
|
|
|
) { |
113
|
|
|
$data = $this->convertThrowableToArray($exception, $context); |
114
|
|
|
|
115
|
|
|
$document = $visitor->getDocument(true); |
|
|
|
|
116
|
|
|
|
117
|
|
|
if (!$visitor->getCurrentNode()) { |
118
|
|
|
$visitor->createRoot(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
foreach ($data as $key => $value) { |
122
|
|
|
$entryNode = $document->createElement($key); |
123
|
|
|
$visitor->getCurrentNode()->appendChild($entryNode); |
124
|
|
|
$visitor->setCurrentNode($entryNode); |
125
|
|
|
|
126
|
|
|
$node = $context->getNavigator()->accept($value, null, $context); |
|
|
|
|
127
|
|
|
if (null !== $node) { |
128
|
|
|
$visitor->getCurrentNode()->appendChild($node); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$visitor->revertCurrentNode(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
3 |
|
protected function convertToArray(\Exception $exception, Context $context): array |
136
|
|
|
{ |
137
|
3 |
|
return $this->convertThrowableToArray($exception, $context); |
138
|
|
|
} |
139
|
|
|
|
140
|
3 |
|
private function convertThrowableToArray(\Throwable $throwable, Context $context): array |
141
|
|
|
{ |
142
|
3 |
|
$data = []; |
143
|
|
|
|
144
|
3 |
|
if ($context->hasAttribute('template_data')) { |
145
|
3 |
|
$templateData = $context->getAttribute('template_data'); |
146
|
|
|
|
147
|
3 |
|
if (array_key_exists('status_code', $templateData)) { |
148
|
3 |
|
$data['code'] = $statusCode = $templateData['status_code']; |
149
|
|
|
} elseif ($context->hasAttribute('status_code')) { |
150
|
3 |
|
$data['code'] = $context->getAttribute('status_code'); |
151
|
|
|
} |
152
|
|
|
} elseif ($context->hasAttribute('status_code')) { |
153
|
|
|
$data['code'] = $context->getAttribute('status_code'); |
154
|
|
|
} |
155
|
|
|
|
156
|
3 |
|
$data['message'] = $this->getMessageFromThrowable($throwable, isset($statusCode) ? $statusCode : null); |
157
|
|
|
|
158
|
3 |
|
return $data; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: