Skip to content

Commit

Permalink
feature #50420 [Console] add support for catching \Throwable errors…
Browse files Browse the repository at this point in the history
… (lyrixx)

This PR was merged into the 6.4 branch.

Discussion
----------

[Console] add support for catching `\Throwable` errors

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #46596
| License       | MIT
| Doc PR        |

Commits
-------

1baeb3d [Console] The application also catch `\Throwable` exceptions
  • Loading branch information
chalasr committed Jul 10, 2023
2 parents a2f75a2 + 1baeb3d commit 43066ff
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/Symfony/Component/Console/Application.php
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) {
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
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)`

6.3
---
Expand Down
34 changes: 33 additions & 1 deletion src/Symfony/Component/Console/Tests/ApplicationTest.php
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

0 comments on commit 43066ff

Please sign in to comment.