Skip to content

Commit b785f13

Browse files
bug #25835 [HttpKernel] DebugHandlersListener should always replace the existing exception handler (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [HttpKernel] DebugHandlersListener should always replace the existing exception handler | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25827 | License | MIT | Doc PR | - The current logic is inconsistent because replacing or not depends whether an ExceptionHandler is registered or not. Embeds tests for the previous PR on the same topic, Debug component's side. Commits ------- a4ddcc2 [HttpKernel] DebugHandlersListener should always replace the existing exception handler
2 parents 78a8a63 + a4ddcc2 commit b785f13

File tree

6 files changed

+87
-7
lines changed

6 files changed

+87
-7
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ install:
181181
elif [[ $deps = low ]]; then
182182
echo "$COMPONENTS" | parallel --gnu -j10% "tfold {} 'cd {} && $COMPOSER_UP --prefer-lowest --prefer-stable && $PHPUNIT_X'"
183183
elif [[ $PHP = hhvm* ]]; then
184-
$PHPUNIT --exclude-group benchmark,intl-data
184+
$PHPUNIT --exclude-group no-hhvm,benchmark,intl-data
185185
else
186186
echo "$COMPONENTS" | parallel --gnu "tfold {} $PHPUNIT_X {}"
187187
tfold tty-group $PHPUNIT --group tty

src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ public function testHandleDeprecation()
293293
@$handler->handleError(E_USER_DEPRECATED, 'Foo deprecation', __FILE__, __LINE__, array());
294294
}
295295

296+
/**
297+
* @group no-hhvm
298+
*/
296299
public function testHandleException()
297300
{
298301
try {
@@ -375,6 +378,9 @@ public function testErrorStacking()
375378
}
376379
}
377380

381+
/**
382+
* @group no-hhvm
383+
*/
378384
public function testHandleFatalError()
379385
{
380386
try {
@@ -434,6 +440,9 @@ public function testHandleErrorException()
434440
$this->assertStringStartsWith("Attempted to load class \"Foo\" from the global namespace.\nDid you forget a \"use\" statement", $args[0]->getMessage());
435441
}
436442

443+
/**
444+
* @group no-hhvm
445+
*/
437446
public function testHandleFatalErrorOnHHVM()
438447
{
439448
try {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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_error_handler('var_dump');
15+
set_exception_handler('var_dump');
16+
17+
ErrorHandler::register(null, false);
18+
19+
if (true) {
20+
class foo extends missing
21+
{
22+
}
23+
}
24+
25+
?>
26+
--EXPECTF--
27+
Fatal error: Class 'Symfony\Component\Debug\missing' not found in %s on line %d
28+
object(Symfony\Component\Debug\Exception\ClassNotFoundException)#%d (8) {
29+
["message":protected]=>
30+
string(131) "Attempted to load class "missing" from namespace "Symfony\Component\Debug".
31+
Did you forget a "use" statement for another namespace?"
32+
["string":"Exception":private]=>
33+
string(0) ""
34+
["code":protected]=>
35+
int(0)
36+
["file":protected]=>
37+
string(%d) "%s"
38+
["line":protected]=>
39+
int(%d)
40+
["trace":"Exception":private]=>
41+
array(0) {
42+
}
43+
["previous":"Exception":private]=>
44+
NULL
45+
["severity":protected]=>
46+
int(1)
47+
}

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

+1-1
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

+7-3
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

+22-2
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)