1313
1414use Psr \Log \LoggerInterface ;
1515use Symfony \Component \Debug \Exception \FlattenException ;
16+ use Symfony \Component \Debug \ExceptionHandler ;
1617use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
1718use Symfony \Component \HttpFoundation \Request ;
19+ use Symfony \Component \HttpFoundation \Response ;
1820use Symfony \Component \HttpKernel \Event \FilterResponseEvent ;
1921use Symfony \Component \HttpKernel \Log \DebugLoggerInterface ;
2022use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
@@ -33,12 +35,17 @@ class ExceptionListener implements EventSubscriberInterface
3335 protected $ controller ;
3436 protected $ logger ;
3537 protected $ debug ;
38+ private $ charset ;
39+ private $ fileLinkFormat ;
40+ private $ isTerminating = false ;
3641
37- public function __construct ($ controller , LoggerInterface $ logger = null , $ debug = false )
42+ public function __construct ($ controller , LoggerInterface $ logger = null , $ debug = false , $ charset = null , $ fileLinkFormat = null )
3843 {
3944 $ this ->controller = $ controller ;
4045 $ this ->logger = $ logger ;
4146 $ this ->debug = $ debug ;
47+ $ this ->charset = $ charset ;
48+ $ this ->fileLinkFormat = $ fileLinkFormat ;
4249 }
4350
4451 public function logKernelException (GetResponseForExceptionEvent $ event )
@@ -51,6 +58,17 @@ public function logKernelException(GetResponseForExceptionEvent $event)
5158
5259 public function onKernelException (GetResponseForExceptionEvent $ event )
5360 {
61+ if (null === $ this ->controller ) {
62+ if (!$ event ->isMasterRequest ()) {
63+ return ;
64+ }
65+ if (!$ this ->isTerminating ) {
66+ $ this ->isTerminating = true ;
67+
68+ return ;
69+ }
70+ $ this ->isTerminating = false ;
71+ }
5472 $ exception = $ event ->getException ();
5573 $ request = $ this ->duplicateRequest ($ exception , $ event ->getRequest ());
5674 $ eventDispatcher = func_num_args () > 2 ? func_get_arg (2 ) : null ;
@@ -86,6 +104,11 @@ public function onKernelException(GetResponseForExceptionEvent $event)
86104 }
87105 }
88106
107+ public function reset ()
108+ {
109+ $ this ->isTerminating = false ;
110+ }
111+
89112 public static function getSubscribedEvents ()
90113 {
91114 return array (
@@ -124,8 +147,12 @@ protected function logException(\Exception $exception, $message)
124147 protected function duplicateRequest (\Exception $ exception , Request $ request )
125148 {
126149 $ attributes = array (
127- '_controller ' => $ this ->controller ,
128- 'exception ' => FlattenException::create ($ exception ),
150+ 'exception ' => $ exception = FlattenException::create ($ exception ),
151+ '_controller ' => $ this ->controller ?: function () use ($ exception ) {
152+ $ handler = new ExceptionHandler ($ this ->debug , $ this ->charset , $ this ->fileLinkFormat );
153+
154+ return new Response ($ handler ->getHtml ($ exception ), $ exception ->getStatusCode (), $ exception ->getHeaders ());
155+ },
129156 'logger ' => $ this ->logger instanceof DebugLoggerInterface ? $ this ->logger : null ,
130157 );
131158 $ request = $ request ->duplicate (null , null , $ attributes );
0 commit comments