@@ -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 }
0 commit comments