@@ -36,6 +36,7 @@ class DebugHandlersListener implements EventSubscriberInterface
36
36
private $ scream ;
37
37
private $ fileLinkFormat ;
38
38
private $ firstCall = true ;
39
+ private $ hasTerminatedWithException ;
39
40
40
41
/**
41
42
* @param callable|null $exceptionHandler A handler that will be called on Exception
@@ -60,14 +61,16 @@ public function __construct($exceptionHandler, LoggerInterface $logger = null, $
60
61
*/
61
62
public function configure (Event $ event = null )
62
63
{
63
- if (!$ this ->firstCall ) {
64
+ if (!$ event instanceof KernelEvent ? ! $ this ->firstCall : ! $ event -> isMasterRequest () ) {
64
65
return ;
65
66
}
66
- $ this ->firstCall = false ;
67
+ $ this ->firstCall = $ this ->hasTerminatedWithException = false ;
68
+
69
+ $ handler = set_exception_handler ('var_dump ' );
70
+ $ handler = is_array ($ handler ) ? $ handler [0 ] : null ;
71
+ restore_exception_handler ();
72
+
67
73
if ($ this ->logger || null !== $ this ->throwAt ) {
68
- $ handler = set_error_handler ('var_dump ' );
69
- $ handler = is_array ($ handler ) ? $ handler [0 ] : null ;
70
- restore_error_handler ();
71
74
if ($ handler instanceof ErrorHandler) {
72
75
if ($ this ->logger ) {
73
76
$ handler ->setDefaultLogger ($ this ->logger , $ this ->levels );
@@ -91,8 +94,16 @@ public function configure(Event $event = null)
91
94
}
92
95
if (!$ this ->exceptionHandler ) {
93
96
if ($ event instanceof KernelEvent) {
94
- if (method_exists ($ event ->getKernel (), 'terminateWithException ' )) {
95
- $ this ->exceptionHandler = array ($ event ->getKernel (), 'terminateWithException ' );
97
+ if (method_exists ($ kernel = $ event ->getKernel (), 'terminateWithException ' )) {
98
+ $ request = $ event ->getRequest ();
99
+ $ hasRun = &$ this ->hasTerminatedWithException ;
100
+ $ this ->exceptionHandler = function (\Exception $ e ) use ($ kernel , $ request , &$ hasRun ) {
101
+ if ($ hasRun ) {
102
+ throw $ e ;
103
+ }
104
+ $ hasRun = true ;
105
+ $ kernel ->terminateWithException ($ e , $ request );
106
+ };
96
107
}
97
108
} elseif ($ event instanceof ConsoleEvent && $ app = $ event ->getCommand ()->getApplication ()) {
98
109
$ output = $ event ->getOutput ();
@@ -105,9 +116,6 @@ public function configure(Event $event = null)
105
116
}
106
117
}
107
118
if ($ this ->exceptionHandler ) {
108
- $ handler = set_exception_handler ('var_dump ' );
109
- $ handler = is_array ($ handler ) ? $ handler [0 ] : null ;
110
- restore_exception_handler ();
111
119
if ($ handler instanceof ErrorHandler) {
112
120
$ h = $ handler ->setExceptionHandler ('var_dump ' ) ?: $ this ->exceptionHandler ;
113
121
$ handler ->setExceptionHandler ($ h );
0 commit comments