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 , $ 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 )
@@ -51,6 +58,17 @@ public function logKernelException(GetResponseForExceptionEvent $event)
51
58
52
59
public function onKernelException (GetResponseForExceptionEvent $ event )
53
60
{
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
+ }
54
72
$ exception = $ event ->getException ();
55
73
$ request = $ this ->duplicateRequest ($ exception , $ event ->getRequest ());
56
74
$ eventDispatcher = func_num_args () > 2 ? func_get_arg (2 ) : null ;
@@ -86,6 +104,11 @@ public function onKernelException(GetResponseForExceptionEvent $event)
86
104
}
87
105
}
88
106
107
+ public function reset ()
108
+ {
109
+ $ this ->isTerminating = false ;
110
+ }
111
+
89
112
public static function getSubscribedEvents ()
90
113
{
91
114
return array (
@@ -124,8 +147,12 @@ protected function logException(\Exception $exception, $message)
124
147
protected function duplicateRequest (\Exception $ exception , Request $ request )
125
148
{
126
149
$ 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
+ },
129
156
'logger ' => $ this ->logger instanceof DebugLoggerInterface ? $ this ->logger : null ,
130
157
);
131
158
$ request = $ request ->duplicate (null , null , $ attributes );
0 commit comments