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 , string $ 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 )
@@ -50,6 +57,17 @@ public function logKernelException(GetResponseForExceptionEvent $event)
5057
5158 public function onKernelException (GetResponseForExceptionEvent $ event )
5259 {
60+ if (null === $ this ->controller ) {
61+ if (!$ event ->isMasterRequest ()) {
62+ return ;
63+ }
64+ if (!$ this ->isTerminating ) {
65+ $ this ->isTerminating = true ;
66+
67+ return ;
68+ }
69+ $ this ->isTerminating = false ;
70+ }
5371 $ exception = $ event ->getException ();
5472 $ request = $ this ->duplicateRequest ($ exception , $ event ->getRequest ());
5573 $ eventDispatcher = func_num_args () > 2 ? func_get_arg (2 ) : null ;
@@ -85,6 +103,11 @@ public function onKernelException(GetResponseForExceptionEvent $event)
85103 }
86104 }
87105
106+ public function reset ()
107+ {
108+ $ this ->isTerminating = false ;
109+ }
110+
88111 public static function getSubscribedEvents ()
89112 {
90113 return array (
@@ -123,8 +146,12 @@ protected function logException(\Exception $exception, $message)
123146 protected function duplicateRequest (\Exception $ exception , Request $ request )
124147 {
125148 $ attributes = array (
126- '_controller ' => $ this ->controller ,
127- 'exception ' => FlattenException::create ($ exception ),
149+ 'exception ' => $ exception = FlattenException::create ($ exception ),
150+ '_controller ' => $ this ->controller ?: function () use ($ exception ) {
151+ $ handler = new ExceptionHandler ($ this ->debug , $ this ->charset , $ this ->fileLinkFormat );
152+
153+ return new Response ($ handler ->getHtml ($ exception ), $ exception ->getStatusCode (), $ exception ->getHeaders ());
154+ },
128155 'logger ' => $ this ->logger instanceof DebugLoggerInterface ? $ this ->logger : null ,
129156 );
130157 $ request = $ request ->duplicate (null , null , $ attributes );
0 commit comments