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

Skip to content

Commit 8fc6dd2

Browse files
committed
[Console] The application also catch \Throwable exceptions
1 parent 6c85287 commit 8fc6dd2

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 13 additions & 1 deletion
8000
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,10 +173,13 @@ public function run(InputInterface $input = null, OutputInterface $output = null
172173

173174
try {
174175
$exitCode = $this->doRun($input, $output);
175-
} catch (\Exception $e) {
176+
} catch (\Throwable $e) {
176177
if (!$this->catchExceptions) {
177178
throw $e;
178179
}
180+
if (!$e instanceof \Exception && !$this->catchErrors) {
181+
throw $e;
182+
}
179183

180184
$renderException($e);
181185

@@ -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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,28 @@ public function testSetCatchExceptions()
798798
}
799799
}
800800

801+
public function testSetCatchErrors()
802+
{
803+
$application = new Application();
804+
$application->setAutoExit(false);
805+
$application->add((new Command('boom'))->setCode(fn () => throw new \Error('This is an error.')));
806+
807+
putenv('COLUMNS=120');
808+
$tester = new ApplicationTester($application);
809+
810+
try {
811+
$tester->run(['command' => 'boom']);
812+
$this->fail('The exception is not catched.');
813+
} catch (\Throwable $e) {
814+
$this->assertInstanceOf(\Error::class, $e);
815+
$this->assertSame('This is an error.', $e->getMessage());
816+
}
817+
818+
$application->setCatchErrors(true);
819+
$tester->run(['command' => 'boom']);
820+
$this->assertStringContainsString(' This is an error.', $tester->getDisplay(true));
821+
}
822+
801823
public function testAutoExitSetting()
802824
{
803825
$application = new Application();

0 commit comments

Comments
 (0)
0