Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UX] Friendly command exceptions output #702

Merged
merged 1 commit into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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