Skip to content

Commit 326a195

Browse files
committed
bug symfony#60507 [Console][Messenger] Fix: Allow UnrecoverableExceptionInterface to bypass retry in RunCommandMessageHandler (santysisi)
This PR was merged into the 6.4 branch. Discussion ---------- [Console][Messenger] Fix: Allow `UnrecoverableExceptionInterface` to bypass retry in `RunCommandMessageHandler` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#60427 | License | MIT This PR ensures that `UnrecoverableExceptionInterface` exceptions thrown during the execution of a command via `RunCommandMessage` are no longer retried. Commits ------- 6d550d8 [Console][Messenger] Fix: Allow UnrecoverableExceptionInterface to bypass retry in RunCommandMessageHandler
2 parents 419006f + 6d550d8 commit 326a195

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
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\RecoverableExceptionInterface;
20+
use Symfony\Component\Messenger\Exception\UnrecoverableExceptionInterface;
1921

2022
/**
2123
* @author Kevin Bond <[email protected]>
@@ -35,6 +37,8 @@ public function __invoke(RunCommandMessage $message): RunCommandContext
3537

3638
try {
3739
$exitCode = $this->application->run($input, $output);
40+
} catch (UnrecoverableExceptionInterface|RecoverableExceptionInterface $e) {
41+
throw $e;
3842
} catch (\Throwable $e) {
3943
throw new RunCommandFailedException($e, new RunCommandContext($message, Command::FAILURE, $output->fetch()));
4044
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
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\RecoverableExceptionInterface;
24+
use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException;
25+
use Symfony\Component\Messenger\Exception\UnrecoverableExceptionInterface;
26+
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
2327

2428
/**
2529
* @author Kevin Bond <[email protected]>
@@ -81,6 +85,38 @@ public function testThrowOnNonSuccess()
8185
$this->fail('Exception not thrown.');
8286
}
8387

88+
public function testExecutesCommandThatThrownUnrecoverableException()
89+
{
90+
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());
91+
92+
try {
93+
$handler(new RunCommandMessage('test:command --throw-unrecoverable'));
94+
} catch (UnrecoverableExceptionInterface $e) {
95+
$this->assertSame('Unrecoverable exception message', $e->getMessage());
96+
$this->assertNull($e->getPrevious());
97+
98+
return;
99+
}
100+
101+
$this->fail('Exception not thrown.');
102+
}
103+
104+
public function testExecutesCommandThatThrownRecoverableException()
105+
{
106+
$handler = new RunCommandMessageHandler($this->createApplicationWithCommand());
107+
108+
try {
109+
$handler(new RunCommandMessage('test:command --throw-recoverable'));
110+
} catch (RecoverableExceptionInterface $e) {
111+
$this->assertSame('Recoverable exception message', $e->getMessage());
112+
$this->assertNull($e->getPrevious());
113+
114+
return;
115+
}
116+
117+
$this->fail('Exception not thrown.');
118+
}
119+
84120
private function createApplicationWithCommand(): Application
85121
{
86122
$application = new Application();
@@ -92,6 +128,8 @@ public function configure(): void
92128
$this
93129
->setName('test:command')
94130
->addOption('throw')
131+
->addOption('throw-unrecoverable')
132+
->addOption('throw-recoverable')
95133
->addOption('exit', null, InputOption::VALUE_REQUIRED, 0)
96134
;
97135
}
@@ -100,6 +138,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
100138
{
101139
$output->write('some message');
102140

141+
if ($input->getOption('throw-unrecoverable')) {
142+
throw new UnrecoverableMessageHandlingException('Unrecoverable exception message');
143+
}
144+
145+
if ($input->getOption('throw-recoverable')) {
146+
throw new RecoverableMessageHandlingException('Recoverable exception message');
147+
}
148+
103149
if ($input->getOption('throw')) {
104150
throw new \RuntimeException('exception message');
105151
}

0 commit comments

Comments
 (0)