8000 [HttpKernel] DebugHandlersListener should always replace the existing… · symfony/symfony@a8dc27b · GitHub
[go: up one dir, main page]

Skip to content

Commit a8dc27b

Browse files
[HttpKernel] DebugHandlersListener should always replace the existing exception handler
1 parent 78a8a63 commit a8dc27b

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
Test catching fatal errors when handlers are nested
3+
--FILE--
4+
<?php
5+
6+
namespace Symfony\Component\Debug;
7+
8+
$vendor = __DIR__;
9+
while (!file_exists($vendor.'/vendor')) {
10+
$vendor = dirname($vendor);
11+
}
12+
require $vendor.'/vendor/autoload.php';
13+
14+
set_exception_handler('var_dump');
15+
16+
ErrorHandler::register();
17+
18+
if (true) {
19+
class foo extends missing
20+
{
21+
}
22+
}
23+
24+
?>
25+
--EXPECTF--
26+
Fatal error: Class 'Symfony\Component\Debug\missing' not found in %s on line %d
27+
object(Symfony\Component\Debug\Exception\ClassNotFoundException)#%d (8) {
28+
["message":protected]=>
29+
string(131) "Attempted to load class "missing" from namespace "Symfony\Component\Debug".
30+
Did you forget a "use" statement for another namespace?"
31+
["string":"Exception":private]=>
32+
string(0) ""
33+
["code":protected]=>
34+
int(0)
35+
["file":protected]=>
36+
string(%d) "%s"
37+
["line":protected]=>
38+
int(%d)
39+
["trace":"Exception":private]=>
40+
array(0) {
41+
}
42+
["previous":"Exception":private]=>
43+
NULL
44+
["severity":protected]=>
45+
int(1)
46+
}

src/Symfony/Component/Debug/Tests/phpt/fatal_with_nested_handlers.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ array(1) {
3535
[0]=>
3636
string(37) "Error and exception handlers do match"
3737
}
38-
object(Symfony\Component\Debug\Exception\FatalErrorException)#4 (8) {
38+
object(Symfony\Component\Debug\Exception\FatalErrorException)#%d (8) {
3939
["message":protected]=>
4040
string(199) "Error: Class Symfony\Component\Debug\Broken contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (Serializable::serialize, Serializable::unserialize)"
4141
%a

src/Symfony/Component/HttpKernel/EventListener/DebugHandlersListener.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,13 @@ public function configure(Event $event = null)
117117
}
118118
if ($this->exceptionHandler) {
119119
if ($handler instanceof ErrorHandler) {
120-
$h = $handler->setExceptionHandler('var_dump') ?: $this->exceptionHandler;
121-
$handler->setExceptionHandler($h);
122-
$handler = is_array($h) ? $h[0] : null;
120+
$h = $handler->setExceptionHandler('var_dump');
121+
if (is_array($h) && $h[0] instanceof ExceptionHandler) {
122+
$handler->setExceptionHandler($h);
123+
$handler = $h[0];
124+
} else {
125+
$handler->setExceptionHandler($this->exceptionHandler);
126+
}
123127
}
124128
if ($handler instanceof ExceptionHandler) {
125129
$handler->setHandler($this->exceptionHandler);

src/Symfony/Component/HttpKernel/Tests/EventListener/DebugHandlersListenerTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
use Symfony\Component\HttpKernel\KernelEvents;
3030

3131
/**
32-
* DebugHandlersListenerTest.
33-
*
3432
* @author Nicolas Grekas <p@tchwork.com>
3533
*/
3634
class DebugHandlersListenerTest extends TestCase
@@ -132,4 +130,26 @@ public function testConsoleEvent()
132130

133131
$xHandler(new \Exception());
134132
}
133+
134+
public function testReplaceExistingExceptionHandler()
135+
{
136+
$userHandler = function () {};
137+
$listener = new DebugHandlersListener($userHandler);
138+
$eHandler = new ErrorHandler();
139+
$eHandler->setExceptionHandler('var_dump');
140+
141+
$exception = null;
142+
set_exception_handler(array($eHandler, 'handleException'));
143+
try {
144+
$listener->configure();
145+
} catch (\Exception $exception) {
146+
}
147+
restore_exception_handler();
148+
149+
if (null !== $exception) {
150+
throw $exception;
151+
}
152+
153+
$this->assertSame($userHandler, $eHandler->setExceptionHandler('var_dump'));
154+
}
135155
}

0 commit comments

Comments
 (0)
0