Skip to content

Commit 7efe526

Browse files
committed
minor #702 [UX] Friendly command exceptions output (yceruto)
This PR was merged into the master branch. Discussion ---------- [UX] Friendly command exceptions output This is only a minor improvement that remove the short trace line where the exception was thrown, thus the output feels like a natural message (intentional) rather than an (unexpected) debuggeable error: **before** ![delete-user-before](https://user-images.githubusercontent.com/2028198/33047796-9a006212-ce25-11e7-9f0c-dccfbf122d7b.png) **after** ![delete-user-after](https://user-images.githubusercontent.com/2028198/33047800-a22f2b80-ce25-11e7-984b-cc48bdcf117d.png) In general, by using the exception classes of the Console component should be better for user messages. Commits ------- 9842a4c Throw Console exceptions for commands
2 parents acf07d1 + 9842a4c commit 7efe526

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/Command/AddUserCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\Utils\Validator;
1616
use Doctrine\ORM\EntityManagerInterface;
1717
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Exception\RuntimeException;
1819
use Symfony\Component\Console\Input\InputArgument;
1920
use Symfony\Component\Console\Input\InputInterface;
2021
use Symfony\Component\Console\Input\InputOption;
@@ -207,7 +208,7 @@ private function validateUserData($username, $plainPassword, $email, $fullName)
207208
$existingUser = $userRepository->findOneBy(['username' => $username]);
208209

209210
if (null !== $existingUser) {
210-
throw new \RuntimeException(sprintf('There is already a user registered with the "%s" username.', $username));
211+
throw new RuntimeException(sprintf('There is already a user registered with the "%s" username.', $username));
211212
}
212213

213214
// validate password and email if is not this input means interactive.
@@ -219,7 +220,7 @@ private function validateUserData($username, $plainPassword, $email, $fullName)
219220
$existingEmail = $userRepository->findOneBy(['email' => $email]);
220221

221222
if (null !== $existingEmail) {
222-
throw new \RuntimeException(sprintf('There is already a user registered with the "%s" email.', $email));
223+
throw new RuntimeException(sprintf('There is already a user registered with the "%s" email.', $email));
223224
}
224225
}
225226

src/Command/DeleteUserCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\Utils\Validator;
1616
use Doctrine\ORM\EntityManagerInterface;
1717
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Exception\RuntimeException;
1819
use Symfony\Component\Console\Input\InputArgument;
1920
use Symfony\Component\Console\Input\InputInterface;
2021
use Symfony\Component\Console\Output\OutputInterface;
@@ -112,7 +113,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
112113
$user = $repository->findOneByUsername($username);
113114

114115
if (null === $user) {
115-
throw new \RuntimeException(sprintf('User with username "%s" not found.', $username));
116+
throw new RuntimeException(sprintf('User with username "%s" not found.', $username));
116117
}
117118

118119
// After an entity has been removed its in-memory state is the same

src/Utils/Validator.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace App\Utils;
1313

14+
use Symfony\Component\Console\Exception\InvalidArgumentException;
15+
1416
/**
1517
* This class is used to provide an example of integrating simple classes as
1618
* services into a Symfony application.
@@ -22,11 +24,11 @@ class Validator
2224
public function validateUsername(?string $username): string
2325
{
2426
if (empty($username)) {
25-
throw new \Exception('The username can not be empty.');
27+
throw new InvalidArgumentException('The username can not be empty.');
2628
}
2729

2830
if (1 !== preg_match('/^[a-z_]+$/', $username)) {
29-
throw new \Exception('The username must contain only lowercase latin characters and underscores.');
31+
throw new InvalidArgumentException('The username must contain only lowercase latin characters and underscores.');
3032
}
3133

3234
return $username;
@@ -35,11 +37,11 @@ public function validateUsername(?string $username): string
3537
public function validatePassword(?string $plainPassword): string
3638
{
3739
if (empty($plainPassword)) {
38-
throw new \Exception('The password can not be empty.');
40+
throw new InvalidArgumentException('The password can not be empty.');
3941
}
4042

4143
if (mb_strlen(trim($plainPassword)) < 6) {
42-
throw new \Exception('The password must be at least 6 characters long.');
44+
throw new InvalidArgumentException('The password must be at least 6 characters long.');
4345
}
4446

4547
return $plainPassword;
@@ -48,11 +50,11 @@ public function validatePassword(?string $plainPassword): string
4850
public function validateEmail(?string $email): string
4951
{
5052
if (empty($email)) {
51-
throw new \Exception('The email can not be empty.');
53+
throw new InvalidArgumentException('The email can not be empty.');
5254
}
5355

5456
if (false === mb_strpos($email, '@')) {
55-
throw new \Exception('The email should look like a real email.');
57+
throw new InvalidArgumentException('The email should look like a real email.');
5658
}
5759

5860
return $email;
@@ -61,7 +63,7 @@ public function validateEmail(?string $email): string
6163
public function validateFullName(?string $fullName): string
6264
{
6365
if (empty($fullName)) {
64-
throw new \Exception('The full name can not be empty.');
66+
throw new InvalidArgumentException('The full name can not be empty.');
6567
}
6668

6769
return $fullName;

0 commit comments

Comments
 (0)