Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] add support for catching \Throwable errors #50420

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Application implements ResetInterface
private string $version;
private ?CommandLoaderInterface $commandLoader = null;
private bool $catchExceptions = true;
private bool $catchErrors = false;
private bool $autoExit = true;
private InputDefinition $definition;
private HelperSet $helperSet;
Expand Down Expand Up @@ -172,8 +173,11 @@ public function run(InputInterface $input = null, OutputInterface $output = null

try {
$exitCode = $this->doRun($input, $output);
} catch (\Exception $e) {
if (!$this->catchExceptions) {
} catch (\Throwable $e) {
if ($e instanceof \Exception && !$this->catchExceptions) {
throw $e;
}
if (!$e instanceof \Exception && !$this->catchErrors) {
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
throw $e;
}

Expand Down Expand Up @@ -427,6 +431,14 @@ public function setCatchExceptions(bool $boolean)
$this->catchExceptions = $boolean;
}

/**
* Sets whether to catch errors or not during commands execution.
*/
public function setCatchErrors(bool $catchErrors = true): void
{
$this->catchErrors = $catchErrors;
}

/**
* Gets whether to automatically exit after a command execution or not.
*/
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add `SignalMap` to map signal value to its name
* Multi-line text in vertical tables is aligned properly
* The application can also catch errors with `Application::setCatchErrors(true)`
lyrixx marked this conversation as resolved.
Show resolved Hide resolved

6.3
---
Expand Down
34 changes: 33 additions & 1 deletion src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,15 @@ public function testFindAmbiguousCommandsIfAllAlternativesAreHidden()
$this->assertInstanceOf(\FooCommand::class, $application->find('foo:'));
}

public function testSetCatchExceptions()
/**
* @testWith [true]
* [false]
*/
public function testSetCatchExceptions(bool $catchErrors)
{
$application = new Application();
$application->setAutoExit(false);
$application->setCatchErrors($catchErrors);
putenv('COLUMNS=120');
$tester = new ApplicationTester($application);

Expand All @@ -798,6 +803,33 @@ public function testSetCatchExceptions()
}
}

/**
* @testWith [true]
* [false]
*/
public function testSetCatchErrors(bool $catchExceptions)
{
$application = new Application();
$application->setAutoExit(false);
$application->setCatchExceptions($catchExceptions);
$application->add((new Command('boom'))->setCode(fn () => throw new \Error('This is an error.')));

putenv('COLUMNS=120');
$tester = new ApplicationTester($application);

try {
$tester->run(['command' => 'boom']);
$this->fail('The exception is not catched.');
} catch (\Throwable $e) {
$this->assertInstanceOf(\Error::class, $e);
$this->assertSame('This is an error.', $e->getMessage());
}

$application->setCatchErrors(true);
$tester->run(['command' => 'boom']);
$this->assertStringContainsString(' This is an error.', $tester->getDisplay(true));
}

public function testAutoExitSetting()
{
$application = new Application();
Expand Down