Skip to content

Commit 16a7a52

Browse files
[FrameworkBundle] enable ErrorHandler in prod
1 parent 5d8e79a commit 16a7a52

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

EventListener/DebugHandlersListener.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\Debug\ErrorHandler;
1616
use Symfony\Component\Debug\ExceptionHandler;
17+
use Symfony\Component\EventDispatcher\Event;
18+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1719
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20+
use Symfony\Component\HttpKernel\Event\KernelEvent;
1821
use Symfony\Component\HttpKernel\KernelEvents;
22+
use Symfony\Component\Console\ConsoleEvents;
23+
use Symfony\Component\Console\Event\ConsoleEvent;
24+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1925

2026
/**
2127
* Configures errors and exceptions handlers.
@@ -28,6 +34,7 @@ class DebugHandlersListener implements EventSubscriberInterface
2834
private $logger;
2935
private $levels;
3036
private $debug;
37+
private $fileLinkFormat;
3138

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

48-
public function configure()
55+
/**
56+
* Configures the error handler.
57+
*
58+
* @param Event|null $event The triggering event
59+
* @param string|null $eventName The triggering event name
60+
* @param EventDispatcherInterface|null $eventDispatcher The dispatcher used to trigger $event
61+
*/
62+
public function configure(Event $event = null, $eventName = null, EventDispatcherInterface $eventDispatcher = null)
4963
{
64+
if (null !== $eventDispatcher) {
65+
foreach (array_keys(static::getSubscribedEvents()) as $name) {
66+
$eventDispatcher->removeListener($name, array($this, 'configure'));
67+
}
68+
}
5069
if ($this->logger) {
5170
$handler = set_error_handler('var_dump', 0);
5271
$handler = is_array($handler) ? $handler[0] : null;
@@ -67,6 +86,19 @@ public function configure()
6786
}
6887
$this->logger = $this->levels = null;
6988
}
89+
if (!$this->exceptionHandler) {
90+
if ($event instanceof KernelEvent) {
91+
$this->exceptionHandler = array($event->getKernel(), 'terminateWithException');
92+
} elseif ($event instanceof ConsoleEvent && $app = $event->getCommand()->getApplication()) {
93+
$output = $event->getOutput();
94+
if ($output instanceof ConsoleOutputInterface) {
95+
$output = $output->getErrorOutput();
96+
}
97+
$this->exceptionHandler = function ($e) use ($app, $output) {
98+
$app->renderException($e, $output);
99+
};
100+
}
101+
}
70102
if ($this->exceptionHandler) {
71103
$handler = set_exception_handler('var_dump');
72104
$handler = is_array($handler) ? $handler[0] : null;
@@ -86,6 +118,12 @@ public function configure()
86118

87119
public static function getSubscribedEvents()
88120
{
89-
return array(KernelEvents::REQUEST => array('configure', 2048));
121+
$events = array(KernelEvents::REQUEST => array('configure', 2048));
122+
123+
if (defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
124+
$events[ConsoleEvents::COMMAND] = array('configure', 2048);
125+
}
126+
127+
return $events;
90128
}
91129
}

Tests/EventListener/DebugHandlersListenerTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212
namespace Symfony\Component\HttpKernel\Tests\EventListener;
1313

1414
use Psr\Log\LogLevel;
15+
use Symfony\Component\Console\Event\ConsoleEvent;
16+
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\ConsoleEvents;
18+
use Symfony\Component\Console\Helper\HelperSet;
19+
use Symfony\Component\Console\Input\ArgvInput;
20+
use Symfony\Component\Console\Output\ConsoleOutput;
1521
use Symfony\Component\Debug\ErrorHandler;
1622
use Symfony\Component\Debug\ExceptionHandler;
23+
use Symfony\Component\EventDispatcher\EventDispatcher;
1724
use Symfony\Component\HttpKernel\EventListener\DebugHandlersListener;
25+
use Symfony\Component\HttpKernel\KernelEvents;
1826

1927
/**
2028
* DebugHandlersListenerTest
@@ -53,4 +61,48 @@ public function testConfigure()
5361
$this->assertArrayHasKey(E_DEPRECATED, $loggers);
5462
$this->assertSame(array($logger, LogLevel::INFO), $loggers[E_DEPRECATED]);
5563
}
64+
65+
public function testConsoleEvent()
66+
{
67+
$dispatcher = new EventDispatcher();
68+
$listener = new DebugHandlersListener(null);
69+
$app = $this->getMock('Symfony\Component\Console\Application');
70+
$app->expects($this->once())->method('getHelperSet')->will($this->returnValue(new HelperSet()));
71+
$command = new Command(__FUNCTION__);
72+
$command->setApplication($app);
73+
$event = new ConsoleEvent($command, new ArgvInput(), new ConsoleOutput());
74+
75+
$dispatcher->addSubscriber($listener);
76+
77+
$xListeners = array(
78+
KernelEvents::REQUEST => array(array($listener, 'configure')),
79+
ConsoleEvents::COMMAND => array(array($listener, 'configure')),
80+
);
81+
$this->assertSame($xListeners, $dispatcher->getListeners());
82+
83+
$exception = null;
84+
$eHandler = new ErrorHandler();
85+
set_error_handler(array($eHandler, 'handleError'));
86+
set_exception_handler(array($eHandler, 'handleException'));
87+
try {
88+
$dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
89+
} catch (\Exception $exception) {
90+
}
91+
restore_exception_handler();
92+
restore_error_handler();
93+
94+
if (null !== $exception) {
95+
throw $exception;
96+
}
97+
98+
$this->assertSame(array(), $dispatcher->getListeners());
99+
100+
$xHandler = $eHandler->setExceptionHandler('var_dump');
101+
$this->assertInstanceOf('Closure', $xHandler);
102+
103+
$app->expects($this->once())
104+
->method('renderException');
105+
106+
$xHandler(new \Exception());
107+
}
56108
}

0 commit comments

Comments
 (0)