Skip to content

Commit

Permalink
minor #702 [UX] Friendly command exceptions output (yceruto)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
javiereguiluz committed Nov 21, 2017
2 parents acf07d1 + 9842a4c commit 7efe526
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/Command/AddUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Utils\Validator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -207,7 +208,7 @@ private function validateUserData($username, $plainPassword, $email, $fullName)
$existingUser = $userRepository->findOneBy(['username' => $username]);

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

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

if (null !== $existingEmail) {
throw new \RuntimeException(sprintf('There is already a user registered with the "%s" email.', $email));
throw new RuntimeException(sprintf('There is already a user registered with the "%s" email.', $email));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Command/DeleteUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Utils\Validator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -112,7 +113,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$user = $repository->findOneByUsername($username);

if (null === $user) {
throw new \RuntimeException(sprintf('User with username "%s" not found.', $username));
throw new RuntimeException(sprintf('User with username "%s" not found.', $username));
}

// After an entity has been removed its in-memory state is the same
Expand Down
16 changes: 9 additions & 7 deletions src/Utils/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace App\Utils;

use Symfony\Component\Console\Exception\InvalidArgumentException;

/**
* This class is used to provide an example of integrating simple classes as
* services into a Symfony application.
Expand All @@ -22,11 +24,11 @@ class Validator
public function validateUsername(?string $username): string
{
if (empty($username)) {
throw new \Exception('The username can not be empty.');
throw new InvalidArgumentException('The username can not be empty.');
}

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

return $username;
Expand All @@ -35,11 +37,11 @@ public function validateUsername(?string $username): string
public function validatePassword(?string $plainPassword): string
{
if (empty($plainPassword)) {
throw new \Exception('The password can not be empty.');
throw new InvalidArgumentException('The password can not be empty.');
}

if (mb_strlen(trim($plainPassword)) < 6) {
throw new \Exception('The password must be at least 6 characters long.');
throw new InvalidArgumentException('The password must be at least 6 characters long.');
}

return $plainPassword;
Expand All @@ -48,11 +50,11 @@ public function validatePassword(?string $plainPassword): string
public function validateEmail(?string $email): string
{
if (empty($email)) {
throw new \Exception('The email can not be empty.');
throw new InvalidArgumentException('The email can not be empty.');
}

if (false === mb_strpos($email, '@')) {
throw new \Exception('The email should look like a real email.');
throw new InvalidArgumentException('The email should look like a real email.');
}

return $email;
Expand All @@ -61,7 +63,7 @@ public function validateEmail(?string $email): string
public function validateFullName(?string $fullName): string
{
if (empty($fullName)) {
throw new \Exception('The full name can not be empty.');
throw new InvalidArgumentException('The full name can not be empty.');
}

return $fullName;
Expand Down

0 comments on commit 7efe526

Please sign in to comment.