1111
1212namespace FOS \RestBundle \Controller ;
1313
14+ use FOS \RestBundle \Exception \FlattenException as FosFlattenException ;
1415use FOS \RestBundle \Util \ExceptionValueMap ;
1516use FOS \RestBundle \View \View ;
1617use FOS \RestBundle \View \ViewHandlerInterface ;
1718use Symfony \Component \HttpFoundation \Request ;
1819use Symfony \Component \HttpFoundation \Response ;
19- use Symfony \Component \Debug \Exception \FlattenException as LegacyFlattenException ;
2020use Symfony \Component \ErrorHandler \Exception \FlattenException ;
2121use Symfony \Component \HttpKernel \Exception \HttpExceptionInterface ;
2222use Symfony \Component \HttpKernel \Log \DebugLoggerInterface ;
@@ -73,7 +73,12 @@ public function showAction(Request $request, $exception, DebugLoggerInterface $l
7373 }
7474 $ templateData = $ this ->getTemplateData ($ currentContent , $ code , $ exception , $ logger );
7575
76- $ view = $ this ->createView ($ exception , $ code , $ templateData , $ request , $ this ->showException );
76+ if ($ exception instanceof \Exception) {
77+ $ view = $ this ->createView ($ exception , $ code , $ templateData , $ request , $ this ->showException );
78+ } else {
79+ $ view = $ this ->createViewFromThrowable ($ exception , $ code , $ templateData , $ request , $ this ->showException );
80+ }
81+
7782 $ response = $ this ->viewHandler ->handle ($ view );
7883
7984 return $ response ;
@@ -90,11 +95,7 @@ public function showAction(Request $request, $exception, DebugLoggerInterface $l
9095 */
9196 protected function createView (\Exception $ exception , $ code , array $ templateData , Request $ request , $ showException )
9297 {
93- $ view = new View ($ exception , $ code , $ exception instanceof HttpExceptionInterface ? $ exception ->getHeaders () : []);
94- $ view ->setTemplateVar ('raw_exception ' );
95- $ view ->setTemplateData ($ templateData );
96-
97- return $ view ;
98+ return $ this ->createViewFromThrowable ($ exception , $ code , $ templateData );
9899 }
99100
100101 /**
@@ -109,22 +110,31 @@ protected function getStatusCode(\Exception $exception)
109110 return $ this ->getStatusCodeFromThrowable ($ exception );
110111 }
111112
113+ private function createViewFromThrowable (\Throwable $ exception , $ code , array $ templateData ): View
114+ {
115+ $ view = new View ($ exception , $ code , $ exception instanceof HttpExceptionInterface ? $ exception ->getHeaders () : []);
116+ $ view ->setTemplateVar ('raw_exception ' );
117+ $ view ->setTemplateData ($ templateData );
118+
119+ return $ view ;
120+ }
121+
112122 /**
113123 * Determines the template parameters to pass to the view layer.
114124 *
115125 * @param string $currentContent
116126 * @param int $code
117- * @param \Exception $exception
127+ * @param \Throwable $throwable
118128 * @param DebugLoggerInterface $logger
119129 *
120130 * @return array
121131 */
122- private function getTemplateData ($ currentContent , $ code , \Exception $ exception , DebugLoggerInterface $ logger = null )
132+ private function getTemplateData ($ currentContent , $ code , \Throwable $ throwable , DebugLoggerInterface $ logger = null )
123133 {
124134 if (class_exists (FlattenException::class)) {
125- $ exception = FlattenException::createFromThrowable ($ exception );
135+ $ exception = FlattenException::createFromThrowable ($ throwable );
126136 } else {
127- $ exception = LegacyFlattenException:: create ( $ exception );
137+ $ exception = FosFlattenException:: createFromThrowable ( $ throwable );
128138 }
129139
130140 return [
0 commit comments