Skip to content

Commit 8f54d4c

Browse files
committed
Fix fatal error when logging console.error without a command
1 parent 451c32a commit 8f54d4c

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

src/Symfony/Component/Console/EventListener/ExceptionListener.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ public function onConsoleError(ConsoleErrorEvent $event)
3939

4040
$error = $event->getError();
4141

42-
$this->logger->error('Error thrown while running command "{command}". Message: "{message}"', array('error' => $error, 'command' => $this->getInputString($event), 'message' => $error->getMessage()));
42+
if (!$inputString = $this->getInputString($event)) {
43+
return $this->logger->error('An error occurred while using the console. Message: "{message}"', array('error' => $error, 'message' => $error->getMessage()));
44+
}
45+
46+
$this->logger->error('Error thrown while running command "{command}". Message: "{message}"', array('error' => $error, 'command' => $inputString, 'message' => $error->getMessage()));
4347
}
4448

4549
public function onConsoleTerminate(ConsoleTerminateEvent $event)
@@ -54,7 +58,11 @@ public function onConsoleTerminate(ConsoleTerminateEvent $event)
5458
return;
5559
}
5660

57-
$this->logger->error('Command "{command}" exited with code "{code}"', array('command' => $this->getInputString($event), 'code' => $exitCode));
61+
if (!$inputString = $this->getInputString($event)) {
62+
return $this->logger->error('The console exited with code "{code}"', array('code' => $exitCode));
63+
}
64+
65+
$this->logger->error('Command "{command}" exited with code "{code}"', array('command' => $inputString, 'code' => $exitCode));
5866
}
5967

6068
public static function getSubscribedEvents()
@@ -67,11 +75,15 @@ public static function getSubscribedEvents()
6775

6876
private static function getInputString(ConsoleEvent $event)
6977
{
70-
$commandName = $event->getCommand()->getName();
78+
$commandName = $event->getCommand() ? $event->getCommand()->getName() : null;
7179
$input = $event->getInput();
7280

7381
if (method_exists($input, '__toString')) {
74-
return str_replace(array("'$commandName'", "\"$commandName\""), $commandName, (string) $input);
82+
if ($commandName) {
83+
return str_replace(array("'$commandName'", "\"$commandName\""), $commandName, (string) $input);
84+
}
85+
86+
return (string) $input;
7587
}
7688

7789
return $commandName;

src/Symfony/Component/Console/Tests/EventListener/ExceptionListenerTest.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\EventListener\ExceptionListener;
2020
use Symfony\Component\Console\Input\ArgvInput;
2121
use Symfony\Component\Console\Input\ArrayInput;
22+
use Symfony\Component\Console\Input\Input;
2223
use Symfony\Component\Console\Input\StringInput;
2324
use Symfony\Component\Console\Input\InputInterface;
2425
use Symfony\Component\Console\Output\OutputInterface;
@@ -36,10 +37,40 @@ public function testOnConsoleError()
3637
->with('Error thrown while running command "{command}". Message: "{message}"', array('error' => $exception, 'command' => 'test:run --foo=baz buzz', 'message' => 'An error occurred'))
3738
;
3839

40+
$listener = new ExceptionListener($logger);
41+
$listener->onConsoleError($this->getConsoleErrorEvent($exception, new ArgvInput(array('console.php', 'test:run', '--foo=baz', 'buzz')), 1, new Command('test:run')));
42+
}
43+
44+
public function testOnConsoleErrorWithNoCommandCastsInputAsString()
45+
{
46+
$exception = new \RuntimeException('An error occurred');
47+
48+
$logger = $this->getLogger();
49+
$logger
50+
->expects($this->once())
51+
->method('error')
52+
->with('Error thrown while running command "{command}". Message: "{message}"', array('error' => $exception, 'command' => '\'test:run\' --foo=baz buzz', 'message' => 'An error occurred'))
53+
;
54+
3955
$listener = new ExceptionListener($logger);
4056
$listener->onConsoleError($this->getConsoleErrorEvent($exception, new ArgvInput(array('console.php', 'test:run', '--foo=baz', 'buzz')), 1));
4157
}
4258

59+
public function testOnConsoleErrorWithNoCommandAndNoInputString()
60+
{
61+
$exception = new \RuntimeException('An error occurred');
62+
63+
$logger = $this->getLogger();
64+
$logger
65+
->expects($this->once())
66+
->method('error')
67+
->with('An error occurred while using the console. Message: "{message}"', array('error' => $exception, 'message' => 'An error occurred'))
68+
;
69+
70+
$listener = new ExceptionListener($logger);
71+
$listener->onConsoleError($this->getConsoleErrorEvent($exception, new NonStringInput(), 1));
72+
}
73+
4374
public function testOnConsoleTerminateForNonZeroExitCodeWritesToLog()
4475
{
4576
$logger = $this->getLogger();
@@ -109,9 +140,9 @@ private function getLogger()
109140
return $this->getMockForAbstractClass(LoggerInterface::class);
110141
}
111142

112-
private function getConsoleErrorEvent(\Exception $exception, InputInterface $input, $exitCode)
143+
private function getConsoleErrorEvent(\Exception $exception, InputInterface $input, $exitCode, Command $command = null)
113144
{
114-
return new ConsoleErrorEvent($input, $this->getOutput(), $exception, $exitCode, new Command('test:run'));
145+
return new ConsoleErrorEvent($input, $this->getOutput(), $exception, $exitCode, $command);
115146
}
116147

117148
private function getConsoleTerminateEvent(InputInterface $input, $exitCode)
@@ -124,3 +155,22 @@ private function getOutput()
124155
return $this->getMockBuilder(OutputInterface::class)->getMock();
125156
}
126157
}
158+
159+
class NonStringInput extends Input
160+
{
161+
public function getFirstArgument()
162+
{
163+
}
164+
165+
public function hasParameterOption($values, $onlyParams = false)
166+
{
167+
}
168+
169+
public function getParameterOption($values, $default = false, $onlyParams = false)
170+
{
171+
}
172+
173+
public function parse()
174+
{
175+
}
176+
}

0 commit comments

Comments
 (0)