10000 [HttpKernel] DebugHandlersListener should always replace the existing exception handler by nicolas-grekas · Pull Request #25835 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] DebugHandlersListener should always replace the existing exception handler #25835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ install:
elif [[ $deps = low ]]; then
echo "$COMPONENTS" | parallel --gnu -j10% "tfold {} 'cd {} && 10000 $COMPOSER_UP --prefer-lowest --prefer-stable && $PHPUNIT_X'"
elif [[ $PHP = hhvm* ]]; then
$PHPUNIT --exclude-group benchmark,intl-data
$PHPUNIT --exclude-group no-hhvm,benchmark,intl-data
else
echo "$COMPONENTS" | parallel --gnu "tfold {} $PHPUNIT_X {}"
tfold tty-group $PHPUNIT --group tty
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ public function testHandleDeprecation()
@$handler->handleError(E_USER_DEPRECATED, 'Foo deprecation', __FILE__, __LINE__, array());
}

/**
* @group no-hhvm
*/
public function testHandleException()
{
try {
Expand Down Expand Up @@ -375,6 +378,9 @@ public function testErrorStacking()
}
}

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

/**
* @group no-hhvm
*/
public function testHandleFatalErrorOnHHVM()
{
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
Test catching fatal errors when handlers are nested
--FILE--
<?php

namespace Symfony\Component\Debug;

$vendor = __DIR__;
while (!file_exists($vendor.'/vendor')) {
$vendor = dirname($vendor);
}
require $vendor.'/vendor/autoload.php';

set_error_handler('var_dump');
set_exception_handler('var_dump');

ErrorHandler::register(null, false);

if (true) {
class foo extends missing
{
}
}

?>
--EXPECTF--
Fatal error: Class 'Symfony\Component\Debug\missing' not found in %s on line %d
object(Symfony\Component\Debug\Exception\ClassNotFoundException)#%d (8) {
["message":protected]=>
string(131) "Attempted to load class "missing" from namespace "Symfony\Component\Debug".
Did you forget a "use" statement for another namespace?"
["string":"Exception":private]=>
string(0) ""
["code":protected]=>
int(0)
["file":protected]=>
string(%d) "%s"
["line":protected]=>
int(%d)
["trace":"Exception":private]=>
array(0) {
}
["previous":"Exception":private]=>
NULL
["severity":protected]=>
int(1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ array(1) {
[0]=>
string(37) "Error and exception handlers do match"
}
object(Symfony\Component\Debug\Exception\FatalErrorException)#4 (8) {
object(Symfony\Component\Debug\Exception\FatalErrorException)#%d (8) {
["message":protected]=>
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)"
%a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ public function configure(Event $event = null)
}
if ($this->exceptionHandler) {
if ($handler instanceof ErrorHandler) {
$h = $handler->setExceptionHandler('var_dump') ?: $this->exceptionHandler;
$handler->setExceptionHandler($h);
$handler = is_array($h) ? $h[0] : null;
$h = $handler->setExceptionHandler('var_dump');
if (is_array($h) && $h[0] instanceof ExceptionHandler) {
$handler->setExceptionHandler($h);
$handler = $h[0];
} else {
$handler->setExceptionHandler($this->exceptionHandler);
}
}
if ($handler instanceof ExceptionHandler) {
$handler->setHandler($this->exceptionHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
use Symfony\Component\HttpKernel\KernelEvents;

/**
* DebugHandlersListenerTest.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class DebugHandlersListenerTest extends TestCase
Expand Down Expand Up @@ -132,4 +130,26 @@ public function testConsoleEvent()

$xHandler(new \Exception());
}

public function testReplaceExistingExceptionHandler()
{
$userHandler = function () {};
$listener = new DebugHandlersListener($userHandler);
$eHandler = new ErrorHandler();
$eHandler->setExceptionHandler('var_dump');

$exception = null;
set_exception_handler(array($eHandler, 'handleException'));
try {
$listener->configure();
} catch (\Exception $exception) {
}
restore_exception_handler();

if (null !== $exception) {
throw $exception;
}

$this->assertSame($userHandler, $eHandler->setExceptionHandler('var_dump'));
}
}
0