Skip to content

Commit 6fc5595

Browse files
committed
bug #2105 handle all Throwable instances during serialization (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- handle all Throwable instances during serialization fixes #2095, replaces #2104 Commits ------- 0e48be8 handle all Throwable instances during serialization
2 parents 565e521 + 0e48be8 commit 6fc5595

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

Serializer/Normalizer/AbstractExceptionNormalizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ public function __construct(ExceptionValueMap $messagesMap, $debug)
4444
/**
4545
* Extracts the exception message.
4646
*
47-
* @param \Exception $exception
47+
* @param \Throwable $throwable
4848
* @param int|null $statusCode
4949
*
5050
* @return string
5151
*/
52-
protected function getExceptionMessage(\Exception $exception, $statusCode = null)
52+
protected function getMessageFromThrowable(\Throwable $throwable, $statusCode = null)
5353
{
54-
$showMessage = $this->messagesMap->resolveException($exception);
54+
$showMessage = $this->messagesMap->resolveThrowable($throwable);
5555

5656
if ($showMessage || $this->debug) {
57-
return $exception->getMessage();
57+
return $throwable->getMessage();
5858
}
5959

6060
return array_key_exists($statusCode, Response::$statusTexts) ? Response::$statusTexts[$statusCode] : 'error';

Serializer/Normalizer/ExceptionHandler.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,24 @@ class ExceptionHandler extends AbstractExceptionNormalizer implements Subscribin
2525
public static function getSubscribingMethods()
2626
{
2727
return [
28+
[
29+
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
30+
'format' => 'json',
31+
'type' => \Error::class,
32+
'method' => 'serializeErrorToJson',
33+
],
2834
[
2935
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
3036
'format' => 'json',
3137
'type' => \Exception::class,
3238
'method' => 'serializeToJson',
3339
],
40+
[
41+
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
42+
'format' => 'xml',
43+
'type' => \Error::class,
44+
'method' => 'serializeErrorToXml',
45+
],
3446
[
3547
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
3648
'format' => 'xml',
@@ -59,6 +71,17 @@ public function serializeToJson(
5971
return $visitor->visitArray($data, $type, $context);
6072
}
6173

74+
public function serializeErrorToJson(
75+
JsonSerializationVisitor $visitor,
76+
\Throwable $exception,
77+
array $type,
78+
Context $context
79+
) {
80+
$data = $this->convertThrowableToArray($exception, $context);
81+
82+
return $visitor->visitArray($data, $type, $context);
83+
}
84+
6285
/**
6386
* @param XmlSerializationVisitor $visitor
6487
* @param \Exception $exception
@@ -93,24 +116,58 @@ public function serializeToXml(
93116
}
94117
}
95118

119+
public function serializeErrorToXml(
120+
XmlSerializationVisitor $visitor,
121+
\Throwable $exception,
122+
array $type,
123+
Context $context
124+
) {
125+
$data = $this->convertThrowableToArray($exception, $context);
126+
127+
$document = $visitor->getDocument(true);
128+
129+
if (!$visitor->getCurrentNode()) {
130+
$visitor->createRoot();
131+
}
132+
133+
foreach ($data as $key => $value) {
134+
$entryNode = $document->createElement($key);
135+
$visitor->getCurrentNode()->appendChild($entryNode);
136+
$visitor->setCurrentNode($entryNode);
137+
138+
$node = $context->getNavigator()->accept($value, null, $context);
139+
if (null !== $node) {
140+
$visitor->getCurrentNode()->appendChild($node);
141+
}
142+
143+
$visitor->revertCurrentNode();
144+
}
145+
}
146+
96147
/**
97148
* @param \Exception $exception
98149
* @param Context $context
99150
*
100151
* @return array
101152
*/
102153
protected function convertToArray(\Exception $exception, Context $context)
154+
{
155+
return $this->convertThrowableToArray($exception, $context);
156+
}
157+
158+
private function convertThrowableToArray(\Throwable $throwable, Context $context): array
103159
{
104160
$data = [];
105161

106162
if ($context->hasAttribute('template_data')) {
107163
$templateData = $context->getAttribute('template_data');
164+
108165
if (array_key_exists('status_code', $templateData)) {
109166
$data['code'] = $statusCode = $templateData['status_code'];
110167
}
111168
}
112169

113-
$data['message'] = $this->getExceptionMessage($exception, isset($statusCode) ? $statusCode : null);
170+
$data['message'] = $this->getMessageFromThrowable($throwable, isset($statusCode) ? $statusCode : null);
114171

115172
return $data;
116173
}

Serializer/Normalizer/ExceptionNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function normalize($object, $format = null, array $context = [])
3131
$data['code'] = $statusCode = $context['template_data']['status_code'];
3232
}
3333

34-
$data['message'] = $this->getExceptionMessage($object, isset($statusCode) ? $statusCode : null);
34+
$data['message'] = $this->getMessageFromThrowable($object, isset($statusCode) ? $statusCode : null);
3535

3636
return $data;
3737
}

0 commit comments

Comments
 (0)