Skip to content
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

[Debug][ErrorHandler] Preserve our error handler when a logger sets another one #29869

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/Debug/ErrorHandler.php
Expand Up @@ -523,6 +523,10 @@ public function handleError($type, $message, $file, $line)
$this->loggers[$type][0]->log($level, $logMessage, $errorAsException ? ['exception' => $errorAsException] : []);
} finally {
$this->isRecursive = false;

if (!\defined('HHVM_VERSION')) {
set_error_handler([$this, __FUNCTION__]);
}
}
}

Expand Down
44 changes: 44 additions & 0 deletions src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php
Expand Up @@ -12,10 +12,13 @@
namespace Symfony\Component\Debug\Tests;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use Symfony\Component\Debug\BufferingLogger;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\Exception\SilencedErrorContext;
use Symfony\Component\Debug\Tests\Fixtures\LoggerThatSetAnErrorHandler;

/**
* ErrorHandlerTest.
Expand Down Expand Up @@ -321,6 +324,8 @@ public function testHandleDeprecation()
$handler = new ErrorHandler();
$handler->setDefaultLogger($logger);
@$handler->handleError(E_USER_DEPRECATED, 'Foo deprecation', __FILE__, __LINE__, []);

restore_error_handler();
}

/**
Expand Down Expand Up @@ -583,4 +588,43 @@ public function testCustomExceptionHandler()

$handler->handleException(new \Exception());
}

/**
* @dataProvider errorHandlerIsNotLostWhenLoggingProvider
*/
public function testErrorHandlerIsNotLostWhenLogging($customErrorHandlerHasBeenPreviouslyDefined, LoggerInterface $logger)
{
try {
if ($customErrorHandlerHasBeenPreviouslyDefined) {
set_error_handler('count');
}

$handler = ErrorHandler::register();
$handler->setDefaultLogger($logger);

@trigger_error('foo', E_USER_DEPRECATED);
@trigger_error('bar', E_USER_DEPRECATED);

$this->assertSame([$handler, 'handleError'], set_error_handler('var_dump'));

restore_error_handler();

if ($customErrorHandlerHasBeenPreviouslyDefined) {
restore_error_handler();
}
} finally {
restore_error_handler();
restore_exception_handler();
}
}

public function errorHandlerIsNotLostWhenLoggingProvider()
{
return [
[false, new NullLogger()],
[true, new NullLogger()],
[false, new LoggerThatSetAnErrorHandler()],
[true, new LoggerThatSetAnErrorHandler()],
];
}
}
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\Debug\Tests\Fixtures;

use Psr\Log\AbstractLogger;

class LoggerThatSetAnErrorHandler extends AbstractLogger
{
public function log($level, $message, array $context = [])
{
set_error_handler('is_string');
restore_error_handler();
}
}