8000 [Console] Fix dispatching throwables from ConsoleEvents::COMMAND · symfony/symfony@0c819b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c819b5

Browse files
[Console] Fix dispatching throwables from ConsoleEvents::COMMAND
1 parent 5742958 commit 0c819b5

File tree

2 files changed

+73
-27
lines changed
  • src/Symfony/Component/Console
    • < 10000 div class="PRIVATE_TreeView-item-level-line prc-TreeView-TreeViewItemLevelLine-KPSSL">
  • Tests
  • 2 files changed

    +73
    -27
    lines changed

    src/Symfony/Component/Console/Application.php

    Lines changed: 29 additions & 27 deletions
    Original file line numberDiff line numberDiff line change
    @@ -117,8 +117,14 @@ public function run(InputInterface $input = null, OutputInterface $output = null
    117117
    $this->configureIO($input, $output);
    118118

    119119
    try {
    120+
    $e = null;
    120121
    $exitCode = $this->doRun($input, $output);
    121122
    } catch (\Exception $e) {
    123+
    } catch (\Throwable $e) {
    124+
    $e = new FatalThrowableError($e);
    125+
    }
    126+
    127+
    if (null !== $e) {
    122128
    if (!$this->catchExceptions) {
    123129
    throw $e;
    124130
    }
    @@ -839,47 +845,43 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
    839845
    }
    840846

    841847
    if (null === $this->dispatcher) {
    842-
    try {
    843-
    return $command->run($input, $output);
    844-
    } catch (\Exception $e) {
    845-
    throw $e;
    846-
    } catch (\Throwable $e) {
    847-
    throw new FatalThrowableError($e);
    848-
    }
    848+
    return $command->run($input, $output);
    849849
    }
    850850

    851851
    $event = new ConsoleCommandEvent($command, $input, $output);
    852-
    $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
    852+
    $e = null;
    853853

    854-
    if ($event->commandShouldRun()) {
    855-
    try {
    856-
    $e = null;
    854+
    try {
    855+
    $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
    856+
    857+
    if ($event->commandShouldRun()) {
    857858
    $exitCode = $command->run($input, $output);
    858-
    } catch (\Exception $x) {
    859-
    $e = $x;
    860-
    } catch (\Throwable $x) {
    861-
    $e = new FatalThrowableError($x);
    859+
    } else {
    860+
    $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
    862861
    }
    863-
    if (null !== $e) {
    864-
    $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
    865-
    $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
    866-
    867-
    if ($e !== $event->getException()) {
    868-
    $x = $e = $event->getException();
    869-
    }
    862+
    } catch (\Exception $x) {
    863+
    $e = $x;
    864+
    } catch (\Throwable $x) {
    865+
    $e = new FatalThrowableError($x);
    866+
    }
    870867

    871-
    $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
    872-
    $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
    868+
    if (null !== $e) {
    869+
    $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
    870+
    $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
    873871

    874-
    throw $x;
    872+
    if ($e !== $event->getException()) {
    873+
    $x = $e = $event->getException();
    875874
    }
    876-
    } else {
    877-
    $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
    875+
    $exitCode = $e->getCode();
    878876
    }
    879877

    880878
    $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
    881879
    $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
    882880

    881+
    if (null !== $e) {
    882+
    throw $x;
    883+
    }
    884+
    883885
    return $event->getExitCode();
    884886
    }
    885887

    src/Symfony/Component/Console/Tests/ApplicationTest.php

    Lines changed: 44 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -977,6 +977,26 @@ public function testRunDispatchesAllEventsWithException()
    977977
    $this->assertContains('before.foo.caught.after.', $tester->getDisplay());
    978978
    }
    979979

    980+
    public function testRunDispatchesAllEventsWithExceptionInListener()
    981+
    {
    982+
    $dispatcher = $this->getDispatcher();
    983+
    $dispatcher->addListener('console.command', function () {
    984+
    throw new \RuntimeException('foo');
    985+
    });
    986+
    987+
    $application = new Application();
    988+
    $application->setDispatcher($dispatcher);
    989+
    $application->setAutoExit(false);
    990+
    991+
    $application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
    992+
    $output->write('foo.');
    993+
    });
    994+
    995+
    $tester = new ApplicationTester($application);
    996+
    $tester->run(array('command' => 'foo'));
    997+
    $this->assertContains('before.caught.after.', $tester->getDisplay());
    998+
    }
    999+
    9801000
    public function testRunWithError()
    9811001
    {
    9821002
    if (method_exists($this, 'expectException')) {
    @@ -1000,6 +1020,30 @@ public function testRunWithError()
    10001020
    $tester->run(array('command' => 'dym'));
    10011021
    }
    10021022

    1023+
    /**
    1024+
    * @requires PHP 7
    1025+
    */
    1026+
    public function testErrorIsRethrownIfNotHandledByConsoleErrorEvent()
    1027+
    {
    1028+
    $application = new Application();
    1029+
    $application->setAutoExit(false);
    1030+
    $application->setDispatcher(new EventDispatcher());
    1031+
    1032+
    $application->register('dym')->setCode(function (InputInterface $input, OutputInterface $output) {
    1033+
    new \UnknownClass();
    1034+
    });
    1035+
    1036+
    $tester = new ApplicationTester($application);
    1037+
    1038+
    try {
    1039+
    $tester->run(array('command' => 'dym'));
    1040+
    $this->fail('->run() should rethrow PHP errors if not handled via ConsoleErrorEvent.');
    1041+
    } catch (\Throwable $e) {
    1042+
    $this->assertInstanceOf('Error', $e);
    1043+
    $this->assertSame($e->getMessage(), 'Class \'UnknownClass\' not found');
    1044+
    }
    1045+
    }
    1046+
    10031047
    /**
    10041048
    * @expectedException \LogicException
    10051049
    * @expectedExceptionMessage caught

    0 commit comments

    Comments
     (0)
    0