Refine error handling

This commit is contained in:
2025-06-07 23:45:40 +02:00
parent 7f93680987
commit 79b24b0d44
2 changed files with 8 additions and 7 deletions

View File

@@ -15,6 +15,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security; use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
@@ -53,7 +54,7 @@ final class RegistrationController extends AbstractController
->subject($this->translator->trans('Please Confirm your Email')) ->subject($this->translator->trans('Please Confirm your Email'))
->htmlTemplate('backoffice/registration/confirmation_email.html.twig'), ->htmlTemplate('backoffice/registration/confirmation_email.html.twig'),
); );
} catch (\Exception $e) { } catch (TransportExceptionInterface $e) {
$logger->error($e->getMessage()); $logger->error($e->getMessage());
} }
$response = $security->login($user, 'form_login', 'main'); $response = $security->login($user, 'form_login', 'main');

View File

@@ -8,18 +8,19 @@ use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mailer\MailerInterface;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface; use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
class EmailVerifier readonly class EmailVerifier
{ {
public function __construct( public function __construct(
private readonly VerifyEmailHelperInterface $verifyEmailHelper, private VerifyEmailHelperInterface $verifyEmailHelper,
private readonly MailerInterface $mailer, private MailerInterface $mailer,
private readonly EntityManagerInterface $entityManager, private EntityManagerInterface $entityManager,
) {} ) {}
/** @throws TransportExceptionInterface */
public function sendEmailConfirmation(string $verifyEmailRouteName, User $user, TemplatedEmail $email): void public function sendEmailConfirmation(string $verifyEmailRouteName, User $user, TemplatedEmail $email): void
{ {
$signatureComponents = $this->verifyEmailHelper->generateSignature( $signatureComponents = $this->verifyEmailHelper->generateSignature(
@@ -39,7 +40,6 @@ class EmailVerifier
$this->mailer->send($email); $this->mailer->send($email);
} }
/** @throws VerifyEmailExceptionInterface */
public function handleEmailConfirmation(Request $request, User $user): void public function handleEmailConfirmation(Request $request, User $user): void
{ {
$this->verifyEmailHelper->validateEmailConfirmationFromRequest($request, (string) $user->getId(), (string) $user->getEmail()); $this->verifyEmailHelper->validateEmailConfirmationFromRequest($request, (string) $user->getId(), (string) $user->getEmail());