|
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
|
|
|
use JMS\Serializer\Context; |
|
15
|
|
|
use JMS\Serializer\GraphNavigator; |
|
16
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
|
17
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
|
18
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
|
19
|
|
|
|
|
20
|
|
|
class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public static function getSubscribingMethods() |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
|
|
[ |
|
29
|
1 |
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
|
30
|
1 |
|
'format' => 'json', |
|
31
|
1 |
|
'type' => \Exception::class, |
|
32
|
1 |
|
'method' => 'serializeToJson', |
|
33
|
1 |
|
], |
|
34
|
|
|
[ |
|
35
|
1 |
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
|
36
|
1 |
|
'format' => 'xml', |
|
37
|
1 |
|
'type' => \Exception::class, |
|
38
|
1 |
|
'method' => 'serializeToXml', |
|
39
|
1 |
|
], |
|
40
|
1 |
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param JsonSerializationVisitor $visitor |
|
45
|
|
|
* @param Exception $exception |
|
46
|
|
|
* @param array $type |
|
47
|
|
|
* @param Context $context |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function serializeToJson( |
|
52
|
|
|
JsonSerializationVisitor $visitor, |
|
53
|
|
|
\Exception $exception, |
|
54
|
|
|
array $type, |
|
55
|
|
|
Context $context |
|
56
|
|
|
) { |
|
57
|
1 |
|
$data = $this->convertToArray($exception, $context); |
|
58
|
|
|
|
|
59
|
1 |
|
return $visitor->visitArray($data, $type, $context); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param XmlSerializationVisitor $visitor |
|
64
|
|
|
* @param Exception $exception |
|
65
|
|
|
* @param array $type |
|
66
|
|
|
* @param Context $context |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function serializeToXml( |
|
69
|
|
|
XmlSerializationVisitor $visitor, |
|
70
|
|
|
\Exception $exception, |
|
71
|
|
|
array $type, |
|
|
|
|
|
|
72
|
|
|
Context $context |
|
73
|
|
|
) { |
|
74
|
1 |
|
$data = $this->convertToArray($exception, $context); |
|
75
|
|
|
|
|
76
|
1 |
View Code Duplication |
if (null === $visitor->document) { |
|
|
|
|
|
|
77
|
1 |
|
$visitor->document = $visitor->createDocument(null, null, true); |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
foreach ($data as $key => $value) { |
|
81
|
1 |
|
$entryNode = $visitor->document->createElement($key); |
|
82
|
1 |
|
$visitor->getCurrentNode()->appendChild($entryNode); |
|
83
|
1 |
|
$visitor->setCurrentNode($entryNode); |
|
84
|
|
|
|
|
85
|
1 |
|
$node = $context->getNavigator()->accept($value, null, $context); |
|
86
|
1 |
|
if (null !== $node) { |
|
87
|
1 |
|
$visitor->getCurrentNode()->appendChild($node); |
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
$visitor->revertCurrentNode(); |
|
91
|
1 |
|
} |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param \Exception $exception |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
2 |
|
protected function convertToArray(\Exception $exception, Context $context) |
|
100
|
|
|
{ |
|
101
|
2 |
|
$data = []; |
|
102
|
|
|
|
|
103
|
2 |
|
$templateData = $context->attributes->get('template_data'); |
|
104
|
2 |
|
if ($templateData->isDefined()) { |
|
105
|
2 |
|
$data['code'] = $statusCode = $templateData->get()['status_code']; |
|
106
|
2 |
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
$data['message'] = $this->getExceptionMessage($exception, isset($statusCode) ? $statusCode : null); |
|
109
|
|
|
|
|
110
|
2 |
|
return $data; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.