Skip to content

Commit c726fc0

Browse files
santysisikbond
andcommitted
[Console][Messenger] Fix: Allow UnrecoverableExceptionInterface to bypass retry in RunCommandMessageHandler
Co-authored-by: Kevin Bond <kevinbond@gmail.com>
1 parent ca85f5f commit c726fc0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Symfony/Component/Console/Messenger/RunCommandMessageHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Exception\RunCommandFailedException;
1717
use Symfony\Component\Console\Input\StringInput;
1818
use Symfony\Component\Console\Output\BufferedOutput;
19+
use Symfony\Component\Messenger\Exception\UnrecoverableExceptionInterface;
1920

2021
/**
2122
* @author Kevin Bond <kevinbond@gmail.com>
@@ -35,6 +36,8 @@ public function __invoke(RunCommandMessage $message): RunCommandContext
3536

3637
try {
3738
$exitCode = $this->application->run($input, $output);
39+
} catch (UnrecoverableExceptionInterface $e) {
40+
throw $e;
3841
} catch (\Throwable $e) {
3942
throw new RunCommandFailedException($e, new RunCommandContext($message, Command::FAILURE, $output->fetch()));
4043
}

src/Symfony/Component/Console/Tests/Messenger/RunCommandMessageHandlerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Symfony\Component\Console\Messenger\RunCommandMessage;
2121
use Symfony\Component\Console\Messenger\RunCommandMessageHandler;
2222
use Symfony\Component\Console\Output\OutputInterface;
23+
use Symfony\Component\Messenger\Exception\UnrecoverableExceptionInterface;
24+
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
2325

2426
/**
2527
* @author Kevin Bond <kevinbond@gmail.com>
@@ -81,6 +83,22 @@ public function testThrowOnNonSuccess()
8183
$this->fail('Exception not thrown.');
8284
}
8385

86+
public function testExecutesCommandThatThrownUnrecoverableException()
87+
{
88+
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());
89+
90+
try {
91+
$handler(new RunCommandMessage('test:command --throw-unrecoverable'));
92+
} catch (UnrecoverableExceptionInterface $e) {
93+
$this->assertSame('unrecoverable exception message', $e->getMessage());
94+
$this->assertNull($e->getPrevious());
95+
96+
return;
97+
}
98+
99+
$this->fail('Exception not thrown.');
100+
}
101+
84102
private function createApplicationWithCommand(): Application
85103
{
86104
$application = new Application();
@@ -92,6 +110,7 @@ public function configure(): void
92110
$this
93111
->setName('test:command')
94112
->addOption('throw')
113+
->addOption('throw-unrecoverable')
95114
->addOption('exit', null, InputOption::VALUE_REQUIRED, 0)
96115
;
97116
}
@@ -100,6 +119,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
100119
{
101120
$output->write('some message');
102121

122+
if ($input->getOption('throw-unrecoverable')) {
123+
throw new UnrecoverableMessageHandlingException('unrecoverable exception message');
124+
}
125+
103126
if ($input->getOption('throw')) {
104127
throw new \RuntimeException('exception message');
105128
}

0 commit comments

Comments
 (0)