refactor: add custom Exception class

This commit is contained in:
Maël Gangloff
2025-10-07 15:55:17 +02:00
parent 79589e63eb
commit ea1df9d97a
10 changed files with 109 additions and 37 deletions

View File

@@ -4,11 +4,14 @@ namespace App\MessageHandler;
use App\Entity\Domain;
use App\Entity\WatchList;
use App\Exception\DomainNotFoundException;
use App\Exception\TldNotSupportedException;
use App\Exception\UnknownRdapServerException;
use App\Message\OrderDomain;
use App\Message\SendDomainEventNotif;
use App\Message\UpdateDomainsFromWatchlist;
use App\Notifier\DomainDeletedNotification;
use App\Notifier\DomainUpdateErrorNotification;
use App\Repository\DomainRepository;
use App\Repository\WatchListRepository;
use App\Service\ChatNotificationService;
use App\Service\Connector\AbstractProvider;
@@ -17,7 +20,6 @@ use App\Service\RDAPService;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;
@@ -39,7 +41,7 @@ final readonly class UpdateDomainsFromWatchlistHandler
private WatchListRepository $watchListRepository,
private LoggerInterface $logger,
#[Autowire(service: 'service_container')]
private ContainerInterface $locator,
private ContainerInterface $locator, private DomainRepository $domainRepository,
) {
$this->sender = new Address($mailerSenderEmail, $mailerSenderName);
}
@@ -95,6 +97,7 @@ final readonly class UpdateDomainsFromWatchlistHandler
foreach ($watchList->getDomains()->filter(fn ($domain) => $domain->isToBeUpdated(false, null !== $watchList->getConnector())) as $domain
) {
$updatedAt = $domain->getUpdatedAt();
$deleted = $domain->getDeleted();
try {
/*
@@ -103,8 +106,10 @@ final readonly class UpdateDomainsFromWatchlistHandler
*/
$this->RDAPService->registerDomain($domain->getLdhName());
$this->bus->dispatch(new SendDomainEventNotif($watchList->getToken(), $domain->getLdhName(), $updatedAt));
} catch (NotFoundHttpException) {
if (!$domain->getDeleted()) {
} catch (DomainNotFoundException) {
$newDomain = $this->domainRepository->findOneBy(['ldhName' => $domain->getLdhName()]);
if (!$deleted && null !== $newDomain && $newDomain->getDeleted()) {
$notification = new DomainDeletedNotification($this->sender, $domain);
$this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage());
$this->chatNotificationService->sendChatNotification($watchList, $notification);
@@ -117,20 +122,10 @@ final readonly class UpdateDomainsFromWatchlistHandler
*/
$this->bus->dispatch(new OrderDomain($watchList->getToken(), $domain->getLdhName()));
}
} catch (\Throwable $e) {
} catch (TldNotSupportedException|UnknownRdapServerException) {
/*
* In case of another unknown error,
* the owner of the Watchlist is informed that an error occurred in updating the domain name.
* In this case, the domain name can no longer be updated. Unfortunately, there is nothing more that can be done.
*/
$this->logger->error('Update error email is sent to user', [
'username' => $watchList->getUser()->getUserIdentifier(),
'error' => $e,
]);
$email = (new DomainUpdateErrorNotification($this->sender, $domain))
->asEmailMessage(new Recipient($watchList->getUser()->getEmail()));
$this->mailer->send($email->getMessage());
throw $e;
}
}
}