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

Skip to content

Commit 7a35645

Browse files
committed
[Console] The application also catch \Throwable exceptions
1 parent a2f75a2 commit 7a35645
< 8000 /pre>

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-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: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,13 +771,19 @@ public function testFindAmbiguousCommandsIfAllAlternativesAreHidden()
771771
$this->assertInstanceOf(\FooCommand::class, $application->find('foo:'));
772772
}
773773

774-
public function testSetCatchExceptions()
774+
/**
775+
* @testWith [true]
776+
* [false]
777+
*/
778+
public function testSetCatchExceptions(bool $catchErrors)
775779
{
776780
$application = new Application();
777781
$application->setAutoExit(false);
782+
$application->setCatchErrors($catchErrors);
778783
putenv('COLUMNS=120');
779784
$tester = new ApplicationTester($application);
780785

786+
781787
$application->setCatchExceptions(true);
782788
$this->assertTrue($application->areExceptionsCaught());
783789

@@ -798,6 +804,33 @@ public function testSetCatchExceptions()
798804
}
799805
}
800806

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

0 commit comments

Comments
 (0)
0