0

I'm writing an AuthService and I don't know how should I show the output. I decided to throw an exception for errors and a simple array for successful messages. I want to know if it's okay or there is a better way.

Let's say we have a function that check's if the email has already exist in DB or not:

    public function checkEmailExist(string $email)
    {
        $user = $this->getUserByEmail($email);

        if ($user) {
            throw new EmailAlreadyExistException();
        }

        return [
            'message' => 'Ok',
        ];
    }

And the exception class defined like this to prevent messing the logs:

use Exception;
use Symfony\Component\HttpFoundation\Response;

class EmailAlreadyExistException extends Exception
{
    public function render()
    {
        return response()->json([
            'message' => __('errors.general'),
            'errors' => [
                'email' => [__('errors.user.email_already_exists')],
            ],
        ], RESPONSE::HTTP_CONFLICT);
    }

    public function report()
    {
    }
}

And the controller:

    public function check(CheckRequest $request)
    {
        return $this->authService->checkEmailExist(
            email: $request->email,
        );
    }
Ali
  • 185
  • 11

1 Answers1

0

To find out if a user with the entered email address already exists, you can use the exists validation rule already present in the framework: https://laravel.com/docs/9.x/validation#rule-exists

A validation error will then be displayed instead of an exception which is absolutely not meaningful to a user.

Then, if you want to catch validation errors to return formatted responses according to your API specifications, you can modify the exception handler of Laravel: https://laravel.com/docs/9.x/errors#rendering-exceptions

use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpFoundation\Response;

///

$this->renderable(function (ValidationException $e, $request) {
    return response()->json([
        'message' => 'validation_rule_failed',
        'errors' => $e->errors(),
    ], Response::HTTP_UNPROCESSABLE_ENTITY);
});
Jedupont
  • 401
  • 1
  • 4
  • 12
  • Thank you! I didn't know we can change the validation error response. Actually I asked the question generally and not specifically for checking if the email already exist. Also, in that exception I am returning a message that says the email has already exist. – Ali May 04 '22 at 12:37
  • @Ali Yes, I understood and that's why I answered in a general way and especially by detailing the best practices to implement. I advise you to do so, the way you do it now adds a lot of code for not much. Knowing that this is already existing in the core of Laravel. If the answer helped you, I invite you to upvote it please. – Jedupont May 04 '22 at 13:06
  • Yeah, but my current task is to create the service class to unify all the logic in a class. Also there are other methods like register and logic in this class that I need to handle the output of them. – Ali May 04 '22 at 13:48