8000 [Console] The application also catch `\Throwable` exceptions · symfony/symfony@0c2552e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c2552e

Browse files
committed
[Console] The application also catch \Throwable exceptions
1 parent a2f75a2 commit 0c2552e

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class Application implements ResetInterface
7979
private string $version;
8080
private ?CommandLoaderInterface $commandLoader = null;
8181
private bool $catchExceptions = true;
82+
private bool $catchErrors = false;
8283
private bool $autoExit = true;
8384
private InputDefinition $definition;
8485
private HelperSet $helperSet;
@@ -172,8 +173,11 @@ public function run(InputInterface $input = null, OutputInterface $output = null
172173

173174
try {
174175
$exitCode = $this->doRun($input, $output);
175-
} catch (\Exception $e) {
176-
if (!$this->catchExceptions) {
176+
} catch (\Throwable $e) {
177+
if ($e instanceof \Exception && !$this->catchExceptions) {
178+
throw $e;
179+
}
180+
if (!$e instanceof \Exception && !$this->catchErrors) {
177181
throw $e;
178182
}
179183

@@ -427,6 +431,14 @@ public function setCatchExceptions(bool $boolean)
427431
$this->catchExceptions = $boolean;
428432
}
429433

434+
/**
435+
* Sets whether to catch errors or not during commands execution.
436+
*/
437+
public function setCatchErrors(bool $catchErrors = true): void
438+
{
439+
$this->catchErrors = $catchErrors;
440+
}
441+
430442
/**
431443
* Gets whether to automatically exit after a command execution or not.
432444
*/

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add `SignalMap` to map signal value to its name
88
* Multi-line text in vertical tables is aligned properly
9+
* The application can also catch errors with `Application::setCatchErrors(true)`
910

1011
6.3
1112
---

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,20 @@ public function testFindAmbiguousCommandsIfAllAlternativesAreHidden()
771771
$this->assertInstanceOf(\FooCommand::class, $application->find('foo:'));
772772
}
773773

774-
public function testSetCatchExceptions()
774+
775+
/**
776+
* @testWith [true]
777+
* [false]
778+
*/
779+
public function testSetCatchExceptions(bool $catchErrors)
775780
{
776781
$application = new Application();
777782
$application->setAutoExit(false);
783+
$application->setCatchErrors($catchErrors);
778784
putenv('COLUMNS=120');
779785
$tester = new ApplicationTester($application);
780786

787+
781788
$application->setCatchExceptions(true);
782789
$this->assertTrue($application->areExceptionsCaught());
783790

@@ -798,6 +805,33 @@ public function testSetCatchExceptions()
798805
}
799806
}
800807

808+
/**
809+
* @testWith [true]
810+
* [false]
811+
*/
812+
public function testSetCatchErrors(bool $catchExceptions)
813+
{
814+
$application = new Application();
815+
$application->setAutoExit(false);
816+
$application->setCatchExceptions($catchExceptions);
817+
$application->add((new Command('boom'))->setCode(fn () => throw new \Error('This is an error.')));
818+
819+
putenv('COLUMNS=120');
820+
$tester = new ApplicationTester($application);
821+
822+
try {
823+
$tester->run(['command' => 'boom']);
824+
$this->fail('The exception is not catched.');
825+
} catch (\Throwable $e) {
826+
$this->assertInstanceOf(\Error::class, $e);
827+
$this->assertSame('This is an error.', $e->getMessage());
828+
}
829+
830+
$application->setCatchErrors(true);
831+
$tester->run(['command' => 'boom']);
832+
$this->assertStringContainsString(' This is an error.', $tester->getDisplay(true));
833+
}
834+
801835
public function testAutoExitSetting()
802836
{
803837
$application = new Application();

0 commit comments

Comments
 (0)
0