Skip to content

Commit c159eaa

Browse files
committed
Make the uncaught exception message of command more friendly
1 parent e247da7 commit c159eaa

1 file changed

Lines changed: 36 additions & 18 deletions

File tree

src/Command.php

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ abstract class Command extends SymfonyCommand
5555
*
5656
* @var array
5757
*/
58-
protected $verbosityMap = [
59-
'v' => OutputInterface::VERBOSITY_VERBOSE,
60-
'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE,
61-
'vvv' => OutputInterface::VERBOSITY_DEBUG,
62-
'quiet' => OutputInterface::VERBOSITY_QUIET,
63-
'normal' => OutputInterface::VERBOSITY_NORMAL,
64-
];
58+
protected $verbosityMap
59+
= [
60+
'v' => OutputInterface::VERBOSITY_VERBOSE,
61+
'vv' => OutputInterface::VERBOSITY_VERY_VERBOSE,
62+
'vvv' => OutputInterface::VERBOSITY_DEBUG,
63+
'quiet' => OutputInterface::VERBOSITY_QUIET,
64+
'normal' => OutputInterface::VERBOSITY_NORMAL,
65+
];
6566

6667
public function __construct(string $name = null)
6768
{
@@ -78,10 +79,7 @@ public function run(InputInterface $input, OutputInterface $output): int
7879
{
7980
$this->output = new SymfonyStyle($input, $output);
8081

81-
return parent::run(
82-
$this->input = $input,
83-
$this->output
84-
);
82+
return parent::run($this->input = $input, $this->output);
8583
}
8684

8785
/**
@@ -94,6 +92,7 @@ public function confirm(string $question, bool $default = false): bool
9492

9593
/**
9694
* Prompt the user for input.
95+
*
9796
* @param null|mixed $default
9897
*/
9998
public function ask(string $question, $default = null)
@@ -103,6 +102,7 @@ public function ask(string $question, $default = null)
103102

104103
/**
105104
* Prompt the user for input with auto completion.
105+
*
106106
* @param null|mixed $default
107107
*/
108108
public function anticipate(string $question, array $choices, $default = null)
@@ -112,6 +112,7 @@ public function anticipate(string $question, array $choices, $default = null)
112112

113113
/**
114114
* Prompt the user for input with auto completion.
115+
*
115116
* @param null|mixed $default
116117
*/
117118
public function askWithCompletion(string $question, array $choices, $default = null)
@@ -137,12 +138,18 @@ public function secret(string $question, bool $fallback = true)
137138

138139
/**
139140
* Give the user a single choice from an array of answers.
141+
*
140142
* @param null|mixed $default
141143
* @param null|mixed $attempts
142144
* @param null|mixed $multiple
143145
*/
144-
public function choice(string $question, array $choices, $default = null, $attempts = null, $multiple = null): string
145-
{
146+
public function choice(
147+
string $question,
148+
array $choices,
149+
$default = null,
150+
$attempts = null,
151+
$multiple = null
152+
): string {
146153
$question = new ChoiceQuestion($question, $choices, $default);
147154

148155
$question->setMaxAttempts($attempts)->setMultiselect($multiple);
@@ -152,6 +159,7 @@ public function choice(string $question, array $choices, $default = null, $attem
152159

153160
/**
154161
* Format input to textual table.
162+
*
155163
* @param mixed $rows
156164
* @param mixed $tableStyle
157165
*/
@@ -174,6 +182,7 @@ public function table(array $headers, $rows, $tableStyle = 'default', array $col
174182

175183
/**
176184
* Write a string as standard output.
185+
*
177186
* @param mixed $string
178187
* @param null|mixed $style
179188
* @param null|mixed $verbosity
@@ -186,6 +195,7 @@ public function line($string, $style = null, $verbosity = null)
186195

187196
/**
188197
* Write a string as information output.
198+
*
189199
* @param mixed $string
190200
* @param null|mixed $verbosity
191201
*/
@@ -196,6 +206,7 @@ public function info($string, $verbosity = null)
196206

197207
/**
198208
* Write a string as comment output.
209+
*
199210
* @param mixed $string
200211
* @param null|mixed $verbosity
201212
*/
@@ -206,6 +217,7 @@ public function comment($string, $verbosity = null)
206217

207218
/**
208219
* Write a string as question output.
220+
*
209221
* @param mixed $string
210222
* @param null|mixed $verbosity
211223
*/
@@ -216,6 +228,7 @@ public function question($string, $verbosity = null)
216228

217229
/**
218230
* Write a string as error output.
231+
*
219232
* @param mixed $string
220233
* @param null|mixed $verbosity
221234
*/
@@ -226,6 +239,7 @@ public function error($string, $verbosity = null)
226239

227240
/**
228241
* Write a string as warning output.
242+
*
229243
* @param mixed $string
230244
* @param null|mixed $verbosity
231245
*/
@@ -240,6 +254,7 @@ public function warn($string, $verbosity = null)
240254

241255
/**
242256
* Write a string in an alert box.
257+
*
243258
* @param mixed $string
244259
*/
245260
public function alert($string)
@@ -258,10 +273,7 @@ public function call(string $command, array $arguments = []): int
258273
{
259274
$arguments['command'] = $command;
260275

261-
return $this->getApplication()->find($command)->run(
262-
$this->createInputFromArguments($arguments),
263-
$this->output
264-
);
276+
return $this->getApplication()->find($command)->run($this->createInputFromArguments($arguments), $this->output);
265277
}
266278

267279
/**
@@ -271,6 +283,7 @@ abstract public function handle();
271283

272284
/**
273285
* Set the verbosity level.
286+
*
274287
* @param mixed $level
275288
*/
276289
protected function setVerbosity($level)
@@ -280,6 +293,7 @@ protected function setVerbosity($level)
280293

281294
/**
282295
* Get the verbosity level in terms of Symfony's OutputInterface level.
296+
*
283297
* @param null|mixed $level
284298
*/
285299
protected function parseVerbosity($level = null): int
@@ -349,6 +363,10 @@ protected function configure()
349363

350364
protected function execute(InputInterface $input, OutputInterface $output)
351365
{
352-
call([$this, 'handle']);
366+
try {
367+
call([$this, 'handle']);
368+
} catch (\Throwable $throwable) {
369+
$this->line(sprintf("<error>[ERROR]</error> Uncaught exception '%s' with message '%s' at %s line %d", '\\' . get_class($throwable), $throwable->getMessage(), $throwable->getFile(), $throwable->getLine()));
370+
}
353371
}
354372
}

0 commit comments

Comments
 (0)