Skip to content

[FrameworkBundle] enable ErrorHandler in prod #12062

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

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ public function load(array $configs, ContainerBuilder $container)
if ($container->getParameter('kernel.debug')) {
$loader->load('debug.xml');

$definition->replaceArgument(0, array(new Reference('http_kernel', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'terminateWithException'));

$definition = $container->findDefinition('http_kernel');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is replaced by a dynamic fetch of the kernel in the configured listener service (see below): $event->getKernel()

$definition->replaceArgument(2, new Reference('debug.controller_resolver'));

Expand All @@ -145,11 +143,15 @@ public function load(array $configs, ContainerBuilder $container)
$container->setAlias('event_dispatcher', 'debug.event_dispatcher');
} else {
$definition->replaceArgument(2, E_COMPILE_ERROR | E_PARSE | E_ERROR | E_CORE_ERROR);

$container->findDefinition('debug.error_handler')->addMethodCall('throwAt', array(0));
}

$this->addClassesToCompile(array(
'Symfony\\Component\\Config\\FileLocator',

'Symfony\\Component\\Debug\\ErrorHandler',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing empty line to keep consistency (group by component)


'Symfony\\Component\\EventDispatcher\\Event',
'Symfony\\Component\\EventDispatcher\\ContainerAwareEventDispatcher',

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<parameters>
<parameter key="debug.debug_handlers_listener.class">Symfony\Component\HttpKernel\EventListener\DebugHandlersListener</parameter>
<parameter key="debug.error_handler.class">Symfony\Component\Debug\ErrorHandler</parameter>
<parameter key="debug.stopwatch.class">Symfony\Component\Stopwatch\Stopwatch</parameter>
</parameters>

Expand All @@ -20,6 +21,8 @@
<argument>null</argument><!-- %templating.helper.code.file_link_format% -->
</service>

<service id="debug.error_handler" class="%debug.error_handler.class%" factory-class="%debug.error_handler.class%" factory-method="register" />

<service id="debug.stopwatch" class="%debug.stopwatch.class%" />
</services>
</container>
10 changes: 8 additions & 2 deletions src/Symfony/Component/Debug/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@ public static function register($levels = -1, $throw = true)

$handler = new static();
$levels &= $handler->thrownErrors;
set_error_handler(array($handler, 'handleError'), $levels);
$prev = set_error_handler(array($handler, 'handleError'), $levels);
$prev = is_array($prev) ? $prev[0] : null;
if ($prev instanceof self) {
restore_error_handler();
$handler = $prev;
} else {
$handler->setExceptionHandler(set_exception_handler(array($handler, 'handleException')));
}
$handler->throwAt($throw ? $levels : 0, true);
$handler->setExceptionHandler(set_exception_handler(array($handler, 'handleException')));

return $handler;
}
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Debug/Tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ public function tearDown()
error_reporting($this->errorReporting);
}

public function testRegister()
{
$handler = ErrorHandler::register();

try {
$this->assertInstanceOf('Symfony\Component\Debug\ErrorHandler', $handler);

try {
$this->assertSame($handler, ErrorHandler::register());
} catch (\Exception $e) {
restore_error_handler();
restore_exception_handler();
}
} catch (\Exception $e) {
}

restore_error_handler();
restore_exception_handler();

if (isset($e)) {
throw $e;
}
}

public function testNotice()
{
ErrorHandler::register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\Console\Output\ConsoleOutputInterface;

/**
* Configures errors and exceptions handlers.
Expand All @@ -28,6 +34,7 @@ class DebugHandlersListener implements EventSubscriberInterface
private $logger;
private $levels;
private $debug;
private $fileLinkFormat;

/**
* @param callable $exceptionHandler A handler that will be called on Exception
Expand All @@ -45,8 +52,20 @@ public function __construct($exceptionHandler, LoggerInterface $logger = null, $
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
}

public function configure()
/**
* Configures the error handler.
*
* @param Event|null $event The triggering event
* @param string|null $eventName The triggering event name
* @param EventDispatcherInterface|null $eventDispatcher The dispatcher used to trigger $event
*/
public function configure(Event $event = null, $eventName = null, EventDispatcherInterface $eventDispatcher = null)
{
if (null !== $eventDispatcher) {
foreach (array_keys(static::getSubscribedEvents()) as $name) {
$eventDispatcher->removeListener($name, array($this, 'configure'));
}
}
if ($this->logger) {
$handler = set_error_handler('var_dump', 0);
$handler = is_array($handler) ? $handler[0] : null;
Expand All @@ -67,6 +86,19 @@ public function configure()
}
$this->logger = $this->levels = null;
}
if (!$this->exceptionHandler) {
if ($event instanceof KernelEvent) {
$this->exceptionHandler = array($event->getKernel(), 'terminateWithException');
} elseif ($event instanceof ConsoleEvent && $app = $event->getCommand()->getApplication()) {
$output = $event->getOutput();
if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
}
$this->exceptionHandler = function ($e) use ($app, $output) {
$app->renderException($e, $output);
};
}
}
if ($this->exceptionHandler) {
$handler = set_exception_handler('var_dump');
$handler = is_array($handler) ? $handler[0] : null;
Expand All @@ -86,6 +118,12 @@ public function configure()

public static function getSubscribedEvents()
{
return array(KernelEvents::REQUEST => array('configure', 2048));
$events = array(KernelEvents::REQUEST => array('configure', 2048));

if (defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
$events[ConsoleEvents::COMMAND] = array('configure', 2048);
}

return $events;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
namespace Symfony\Component\HttpKernel\Tests\EventListener;

use Psr\Log\LogLevel;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\EventListener\DebugHandlersListener;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* DebugHandlersListenerTest
Expand Down Expand Up @@ -53,4 +61,48 @@ public function testConfigure()
$this->assertArrayHasKey(E_DEPRECATED, $loggers);
$this->assertSame(array($logger, LogLevel::INFO), $loggers[E_DEPRECATED]);
}

public function testConsoleEvent()
{
$dispatcher = new EventDispatcher();
$listener = new DebugHandlersListener(null);
$app = $this->getMock('Symfony\Component\Console\Application');
$app->expects($this->once())->method('getHelperSet')->will($this->returnValue(new HelperSet()));
$command = new Command(__FUNCTION__);
$command->setApplication($app);
$event = new ConsoleEvent($command, new ArgvInput(), new ConsoleOutput());

$dispatcher->addSubscriber($listener);

$xListeners = array(
KernelEvents::REQUEST => array(array($listener, 'configure')),
ConsoleEvents::COMMAND => array(array($listener, 'configure')),
);
$this->assertSame($xListeners, $dispatcher->getListeners());

$exception = null;
$eHandler = new ErrorHandler();
set_error_handler(array($eHandler, 'handleError'));
set_exception_handler(array($eHandler, 'handleException'));
try {
$dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
} catch (\Exception $exception) {
}
restore_exception_handler();
restore_error_handler();

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

$this->assertSame(array(), $dispatcher->getListeners());

$xHandler = $eHandler->setExceptionHandler('var_dump');
$this->assertInstanceOf('Closure', $xHandler);

$app->expects($this->once())
->method('renderException');

$xHandler(new \Exception());
}
}