8000 feature #14894 [Console] Add domain exceptions to replace generic exc… · symfony/symfony@dacbfe9 · GitHub
[go: up one dir, main page]

Skip to content

Commit dacbfe9

Browse files
committed
feature #14894 [Console] Add domain exceptions to replace generic exceptions (GromNaN)
This PR was squashed before being merged into the 2.8 branch (closes #14894). Discussion ---------- [Console] Add domain exceptions to replace generic exceptions Creates domain specific exception classes for the case where a user type an invalid command name or option name. | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #14873 | License | MIT | Doc PR | N/A TODO: * [x] Replace `\InvalidArgumentException` by `Symfony\Component\Console\Exception\InvalidArgumentException` * [x] Add `Symfony\Component\Console\Exception\ExceptionInterface` Commits ------- dd17dc0 [Console] Add domain exceptions to replace generic exceptions
2 parents d60428c + dd17dc0 commit dacbfe9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+411
-206
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
use Symfony\Component\Console\Event\ConsoleCommandEvent;
3939
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
4040
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
41+
use Symfony\Component\Console\Exception\CommandNotFoundException;
42+
use Symfony\Component\Console\Exception\LogicException;
4143
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
4244

4345
/**
@@ -392,7 +394,7 @@ public function add(Command $command)
392394
}
393395

394396
if (null === $command->getDefinition()) {
395-
throw new \LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
397+
throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
396398
}
397399

398400
$this->commands[$command->getName()] = $command;
@@ -411,14 +413,14 @@ public function add(Command $command)
411413
*
412414
* @return Command A Command object
413415
*
414-
* @throws \InvalidArgumentException When command name given does not exist
416+
* @throws CommandNotFoundException When command name given does not exist
415417
*
416418
* @api
417419
*/
418420
public function get($name)
419421
{
420422
if (!isset($this->commands[$name])) {
421-
throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name));
423+
throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name));
422424
}
423425

424426
$command = $this->commands[$name];
@@ -477,7 +479,7 @@ public function getNamespaces()
477479
*
478480
* @return string A registered namespace
479481
*
480-
* @throws \InvalidArgumentException When namespace is incorrect or ambiguous
482+
* @throws CommandNotFoundException When namespace is incorrect or ambiguous
481483
*/
482484
public function findNamespace($namespace)
483485
{
@@ -498,12 +500,12 @@ public function findNamespace($namespace)
498500
$message .= implode("\n ", $alternatives);
499501
}
500502

501-
throw new \InvalidArgumentException($message);
503+
throw new CommandNotFoundException($message, $alternatives);
502504
}
503505

504506
$exact = in_array($namespace, $namespaces, true);
505507
if (count($namespaces) > 1 && !$exact) {
506-
throw new \InvalidArgumentException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))));
508+
throw new CommandNotFoundException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))), array_values($namespaces));
507509
}
508510

509511
return $exact ? $namespace : reset($namespaces);
@@ -519,7 +521,7 @@ public function findNamespace($namespace)
519521
*
520522
* @return Command A Command instance
521523
*
522-
* @throws \InvalidArgumentException When command name is incorrect or ambiguous
524+
* @throws CommandNotFoundException When command name is incorrect or ambiguous
523525
*
524526
* @api
525527
*/
@@ -546,7 +548,7 @@ public function find($name)
546548
$message .= implode("\n ", $alternatives);
547549
}
548550

549-
throw new \InvalidArgumentException($message);
551+
throw new CommandNotFoundException($message, $alternatives);
550552
}
551553

552554
// filter out aliases for commands which are already on the list
@@ -563,7 +565,7 @@ public function find($name)
563565
if (count($commands) > 1 && !$exact) {
564566
$suggestions = $this->getAbbreviationSuggestions(array_values($commands));
565567

566-
throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions));
568+
throw new CommandNotFoundException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions), array_values($commands));
567569
}
568570

569571
return $this->get($exact ? $name : reset($commands));

src/Symfony/Component/Console/Command/Command.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Symfony\Component\Console\Output\OutputInterface;
2222
use Symfony\Component\Console\Application;
2323
use Symfony\Component\Console\Helper\HelperSet;
24+
use Symfony\Component\Console\Exception\InvalidArgumentException;
25+
use Symfony\Component\Console\Exception\LogicException;
2426

2527
/**
2628
* Base class for all commands.
@@ -51,7 +53,7 @@ class Command
5153
*
5254
* @param string|null $name The name of the command; passing null means it must be set in configure()
5355
*
54-
* @throws \LogicException When the command name is empty
56+
* @throws LogicException When the command name is empty
5557
*
5658
* @api
5759
*/
@@ -66,7 +68,7 @@ public function __construct($name = null)
6668
$this->configure();
6769

6870
if (!$this->name) {
69-
throw new \LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_class($this)));
71+
throw new LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_class($this)));
7072
}
7173
}
7274

@@ -162,13 +164,13 @@ protected function configure()
162164
*
163165
* @return null|int null or 0 if everything went fine, or an error code
164166
*
165-
* @throws \LogicException When this abstract method is not implemented
167+
* @throws LogicException When this abstract method is not implemented
166168
*
167169
* @see setCode()
168170
*/
169171
protected function execute(InputInterface $input, OutputInterface $output)
170172
{
171-
throw new \LogicException('You must override the execute() method in the concrete command class.');
173+
throw new LogicException('You must override the execute() method in the concrete command class.');
172174
}
173175

174176
/**
@@ -272,7 +274,7 @@ public function run(InputInterface $input, OutputInterface $output)
272274
*
273275
* @return Command The current instance
274276
*
275-
* @throws \InvalidArgumentException
277+
* @throws InvalidArgumentException
276278
*
277279
* @see execute()
278280
*
@@ -281,7 +283,7 @@ public function run(InputInterface $input, OutputInterface $output)
281283
public function setCode($code)
282284
{
283285
if (!is_callable($code)) {
284-
throw new \InvalidArgumentException('Invalid callable provided to Command::setCode.');
286+
throw new InvalidArgumentException('Invalid callable provided to Command::setCode.');
285287
}
286288

287289
if (PHP_VERSION_ID >= 50400 && $code instanceof \Closure) {
@@ -423,7 +425,7 @@ public function addOption($name, $shortcut = null, $mode = null, $description =
423425
*
424426
* @return Command The current instance
425427
*
426-
* @throws \InvalidArgumentException When the name is invalid
428+
* @throws InvalidArgumentException When the name is invalid
427429
*
428430
* @api
429431
*/
@@ -552,14 +554,14 @@ public function getProcessedHelp()
552554
*
553555
* @return Command The current instance
554556
*
555-
* @throws \InvalidArgumentException When an alias is invalid
557+
* @throws InvalidArgumentException When an alias is invalid
556558
*
557559
* @api
558560
*/
559561
public function setAliases($aliases)
560562
{
561563
if (!is_array($aliases) && !$aliases instanceof \Traversable) {
562-
throw new \InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
564+
throw new InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
563565
}
564566

565567
foreach ($aliases as $alias) {
@@ -634,7 +636,7 @@ public function getUsages()
634636
*
635637
* @return mixed The helper value
636638
*
637-
* @throws \InvalidArgumentException if the helper is not defined
639+
* @throws InvalidArgumentException if the helper is not defined
638640
*
639641
* @api
640642
*/
@@ -693,12 +695,12 @@ public function asXml($asDom = false)
693695
*
694696
* @param string $name
695697
*
696-
* @throws \InvalidArgumentException When the name is invalid
698+
* @throws InvalidArgumentException When the name is invalid
697699
*/
698700
private function validateName($name)
699701
{
700702
if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) {
701-
throw new \InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
703+
throw new InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
702704
}
703705
}
704706
}

src/Symfony/Component/Console/Descriptor/ApplicationDescription.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Console\Application;
1515
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Exception\CommandNotFoundException;
1617

1718
/**
1819
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
@@ -89,12 +90,12 @@ public function getCommands()
8990
*
9091
* @return Command
9192
*
92-
* @throws \InvalidArgumentException
93+
* @throws CommandNotFoundException
9394
*/
9495
public function getCommand($name)
9596
{
9697
if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
97-
throw new \InvalidArgumentException(sprintf('Command %s does not exist.', $name));
98+
throw new CommandNotFoundException(sprintf('Command %s does not exist.', $name));
9899
}
99100

100101
return isset($this->commands[$name]) ? $this->commands[$name] : $this->aliases[$name];

src/Symfony/Component/Console/Descriptor/Descriptor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Input\InputDefinition;
1818
use Symfony\Component\Console\Input\InputOption;
1919
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Exception\InvalidArgumentException;
2021

2122
/**
2223
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
@@ -54,7 +55,7 @@ public function describe(OutputInterface $output, $object, array $options = arra
5455
$this->describeApplication($object, $options);
5556
break;
5657
default:
57-
throw new \InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_class($object)));
58+
throw new InvalidArgumentException(sprintf('Object of type "%s" is not describable.', get_class($object)));
5859
}
5960
}
6061

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
/**
15+
* Represents an incorrect command name typed in the console.
16+
*
17+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
18+
*/
19+
class CommandNotFoundException extends \InvalidArgumentException implements ExceptionInterface
20+
{
21+
private $alternatives;
22+
23+
/**
24+
* @param string $message Exception message to throw.
25+
* @param array $alternatives List of similar defined names.
26+
* @param int $code Exception code.
27+
* @param Exception $previous previous exception used for the exception chaining.
28+
*/
29+
public function __construct($message, array $alternatives = array(), $code = 0, \Exception $previous = null)
30+
{
31+
parent::__construct($message, $code, $previous);
32+
33+
$this->alternatives = $alternatives;
34+
}
35+
36+
/**
37+
* @return array A list of similar defined names.
38+
*/
39+
public function getAlternatives()
40+
{
41+
return $this->alternatives;
42+
}
43+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
/**
15+
* ExceptionInterface.
16+
*
17+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
18+
*
19+
* @api
20+
*/
21+
interface ExceptionInterface
22+
{
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
/**
15+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
16+
*/
17+
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
18+
{
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
/**
15+
* Represents an incorrect option name typed in the console.
16+
*
17+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
18+
*/
19+
class InvalidOptionException extends \InvalidArgumentException implements ExceptionInterface
20+
{
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
/**
15+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
16+
*/
17+
class LogicException extends \LogicException implements ExceptionInterface
18+
{
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
/**
15+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
16+
*/
17+
class RuntimeException extends \RuntimeException implements ExceptionInterface
18+
{
19+
}

0 commit comments

Comments
 (0)
0