Skip to content

Commit 50b538e

Browse files
[Console] Fix using finally where the catch can also fail
1 parent 6ed867e commit 50b538e

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

Application.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
874874
}
875875

876876
$event = new ConsoleCommandEvent($command, $input, $output);
877+
$e = null;
877878

878879
try {
879880
$this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
@@ -886,13 +887,18 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
886887
} catch (\Throwable $e) {
887888
$event = new ConsoleErrorEvent($input, $output, $e, $command);
888889
$this->dispatcher->dispatch(ConsoleEvents::ERROR, $event);
890+
$e = $event->getError();
889891

890-
if (0 !== $exitCode = $event->getExitCode()) {
891-
throw $event->getError();
892+
if (0 === $exitCode = $event->getExitCode()) {
893+
$e = null;
892894
}
893-
} finally {
894-
$event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
895-
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
895+
}
896+
897+
$event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
898+
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
899+
900+
if (null !== $e) {
901+
throw $e;
896902
}
897903

898904
return $event->getExitCode();

Tests/ApplicationTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,34 @@ public function testErrorIsRethrownIfNotHandledByConsoleErrorEventWithCatchingEn
15531553
}
15541554
}
15551555

1556+
/**
1557+
* @expectedException \RuntimeException
1558+
* @expectedExceptionMessage foo
1559+
*/
1560+
public function testThrowingErrorListener()
1561+
{
1562+
$dispatcher = $this->getDispatcher();
1563+
$dispatcher->addListener('console.error', function (ConsoleErrorEvent $event) {
1564+
throw new \RuntimeException('foo');
1565+
});
1566+
1567+
$dispatcher->addListener('console.command', function () {
1568+
throw new \RuntimeException('bar');
1569+
});
1570+
1571+
$application = new Application();
1572+
$application->setDispatcher($dispatcher);
1573+
$application->setAutoExit(false);
1574+
$application->setCatchExceptions(false);
1575+
1576+
$application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
1577+
$output->write('foo.');
1578+
});
1579+
1580+
$tester = new ApplicationTester($application);
1581+
$tester->run(array('command' => 'foo'));
1582+
}
1583+
15561584
protected function tearDown()
15571585
{
15581586
putenv('SHELL_VERBOSITY');

0 commit comments

Comments
 (0)