8000 Console: catch Throwable · symfony/symfony@fbbbc9a · GitHub
[go: up one dir, main page]

Skip to content

Commit fbbbc9a

Browse files
committed
Console: catch Throwable
1 parent 646d045 commit fbbbc9a

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Console\Descriptor\TextDescriptor;
1515
use Symfony\Component\Console\Descriptor\XmlDescriptor;
1616
use Symfony\Component\Console\Exception\ExceptionInterface;
17+
use Symfony\Component\Console\Exception\FatalThrowableError;
1718
use Symfony\Component\Console\Helper\DebugFormatterHelper;
1819
use Symfony\Component\Console\Helper\ProcessHelper;
1920
use Symfony\Component\Console\Helper\QuestionHelper;
@@ -119,9 +120,15 @@ public function run(InputInterface $input = null, OutputInterface $output = null
119120

120121
$this->configureIO($input, $output);
121122

123+
unset($e);
122124
try {
123125
$exitCode = $this->doRun($input, $output);
124126
} catch (\Exception $e) {
127+
} catch (\Throwable $e) {
128+
$e = new FatalThrowableError($e);
129+
}
130+
131+
if (isset($e)) {
125132
if (!$this->catchExceptions) {
126133
throw $e;
127134
}
@@ -856,9 +863,15 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
856863
$this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
857864

858865
if ($event->commandShouldRun()) {
866+
unset($e);
859867
try {
860868
$exitCode = $command->run($input, $output);
861869
} catch (\Exception $e) {
870+
} catch (\Throwable $e) {
871+
$e = new FatalThrowableError($e);
872+
}
873+
874+
if (isset($e)) {
862875
$event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode());
863876
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
864877

@@ -869,6 +882,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
869882

870883
throw $e;
871884
}
885+
872886
} else {
873887
$exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
874888
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Exception;
13+
14+
class FatalThrowableError extends \ErrorException
15+
{
16+
public function __construct(\Throwable $e)
17+
{
18+
if ($e instanceof \ParseError) {
19+
$message = 'Parse error: '.$e->getMessage();
20+
$severity = E_PARSE;
21+
} elseif ($e instanceof \TypeError) {
22+
$message = 'Type error: '.$e->getMessage();
23+
$severity = E_RECOVERABLE_ERROR;
24+
} else {
25+
$message = 'Fatal error: '.$e->getMessage();
26+
$severity = E_ERROR;
27+
}
28+
29+
\ErrorException::__construct(
30+
$message,
31+
$e->getCode(),
32+
$severity,
33+
$e->getFile(),
34+
$e->getLine()
35+
);
36+
37+
$this->setTrace($e->getTrace());
38+
}
39+
40+
protected function setTrace($trace)
41+
{
42+
$traceReflector = new \ReflectionProperty('Exception', 'trace');
43+
$traceReflector->setAccessible(true);
44+
$traceReflector->setValue($this, $trace);
45+
}
46+
}

src/Symfony/Component/Console/Helper/DialogHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ private function validateAttempts($interviewer, OutputInterface $output, $valida
494494
try {
495495
return call_user_func($validator, $interviewer());
496496
} catch (\Exception $e) {
497+
} catch (\Throwable $e) {
497498
}
498499
}
499500

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ private function validateAttempts($interviewer, OutputInterface $output, Questio
398398
try {
399399
return call_user_func($question->getValidator(), $interviewer());
400400
} catch (\Exception $error) {
401+
} catch (\Throwable $error) {
401402
}
402403
}
403404

src/Symfony/Component/Console/Shell.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ private function autocompleter($text)
189189
$command = $this->application->find(substr($text, 0, strpos($text, ' ')));
190190
} catch (\Exception $e) {
191191
return true;
192+
} catch (\Throwable $e) {
193+
return true;
192194
}
193195

194196
$list = array('--help');

0 commit comments

Comments
 (0)
0