13
13
14
14
use Psr \Log \LoggerInterface ;
15
15
use Symfony \Component \Debug \Exception \FlattenException ;
16
+ use Symfony \Component \Debug \ExceptionHandler ;
16
17
use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
17
18
use Symfony \Component \HttpFoundation \Request ;
19
+ use Symfony \Component \HttpFoundation \Response ;
18
20
use Symfony \Component \HttpKernel \Event \FilterResponseEvent ;
19
21
use Symfony \Component \HttpKernel \Log \DebugLoggerInterface ;
20
22
use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
@@ -33,12 +35,17 @@ class ExceptionListener implements EventSubscriberInterface
33
35
protected $ controller ;
34
36
protected $ logger ;
35
37
protected $ debug ;
38
+ private $ charset ;
39
+ private $ fileLinkFormat ;
40
+ private $ isTerminating = false ;
36
41
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 )
38
43
{
39
44
$ this ->controller = $ controller ;
40
45
$ this ->logger = $ logger ;
41
46
$ this ->debug = $ debug ;
47
+ $ this ->charset = $ charset ;
48
+ $ this ->fileLinkFormat = $ fileLinkFormat ;
42
49
}
43
50
44
51
public function logKernelException (GetResponseForExceptionEvent $ event )
@@ -50,6 +57,17 @@ public function logKernelException(GetResponseForExceptionEvent $event)
50
57
51
58
public function onKernelException (GetResponseForExceptionEvent $ event )
52
59
{
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
+ }
53
71
$ exception = $ event ->getException ();
54
72
$ request = $ this ->duplicateRequest ($ exception , $ event ->getRequest ());
55
73
$ eventDispatcher = func_num_args () > 2 ? func_get_arg (2 ) : null ;
@@ -85,6 +103,11 @@ public function onKernelException(GetResponseForExceptionEvent $event)
85
103
}
86
104
}
87
105
106
+ public function reset ()
107
+ {
108
+ $ this ->isTerminating = false ;
109
+ }
110
+
88
111
public static function getSubscribedEvents ()
89
112
{
90
113
return array (
@@ -123,8 +146,12 @@ protected function logException(\Exception $exception, $message)
123
146
protected function duplicateRequest (\Exception $ exception , Request $ request )
124
147
{
125
148
$ 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
+ },
128
155
'logger ' => $ this ->logger instanceof DebugLoggerInterface ? $ this ->logger : null ,
129
156
);
130
157
$ request = $ request ->duplicate (null , null , $ attributes );
0 commit comments