feat: add error handling

This commit is contained in:
Maël Gangloff 2024-07-14 11:20:04 +02:00
parent 24d21fbc88
commit d202aa8964
No known key found for this signature in database
GPG Key ID: 11FDC81C24A7F629
2 changed files with 30 additions and 7 deletions

View File

@ -4,6 +4,7 @@
namespace App\Controller; namespace App\Controller;
use App\Service\RDAPService; use App\Service\RDAPService;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
@ -22,7 +23,11 @@ class TestController extends AbstractController
#[Route(path: '/test/register/{fqdn}', name: 'test_register_domain')] #[Route(path: '/test/register/{fqdn}', name: 'test_register_domain')]
public function testRegisterDomain(string $fqdn): Response public function testRegisterDomain(string $fqdn): Response
{ {
$this->RDAPService->registerDomain($fqdn); try {
$this->RDAPService->registerDomain($fqdn);
} catch (Exception $e) {
return new Response($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
return new Response(); return new Response();
} }

View File

@ -25,6 +25,7 @@ use Doctrine\ORM\EntityManagerInterface;
use Exception; use Exception;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
use Throwable;
class RDAPService class RDAPService
{ {
@ -44,14 +45,25 @@ class RDAPService
} }
/**
* @throws Exception
*/
public function registerDomain(string $fqdn): Domain public function registerDomain(string $fqdn): Domain
{ {
$idnDomain = idn_to_ascii($fqdn); $idnDomain = idn_to_ascii($fqdn);
$rdapServer = $this->getRDAPServer(RDAPService::getTld($idnDomain)); try {
$rdapServer = $this->getRDAPServer(RDAPService::getTld($idnDomain));
} catch (Exception $e) {
throw new Exception("Unable to determine which RDAP server to contact");
}
$res = $this->client->request( try {
'GET', $rdapServer . 'domain/' . $idnDomain $res = $this->client->request(
)->toArray(); 'GET', $rdapServer . 'domain/' . $idnDomain
)->toArray();
} catch (Throwable $e) {
throw new Exception("Unable to contact RDAP server");
}
$domain = $this->domainRepository->findOneBy(["ldhName" => strtolower($res['ldhName'])]); $domain = $this->domainRepository->findOneBy(["ldhName" => strtolower($res['ldhName'])]);
if ($domain === null) $domain = new Domain(); if ($domain === null) $domain = new Domain();
@ -149,6 +161,9 @@ class RDAPService
} }
/**
* @throws Exception
*/
private function getRDAPServer(string $tld) private function getRDAPServer(string $tld)
{ {
@ -156,18 +171,21 @@ class RDAPService
foreach ($dnsRoot as $dns) { foreach ($dnsRoot as $dns) {
if (in_array($tld, $dns[0])) return $dns[1][0]; if (in_array($tld, $dns[0])) return $dns[1][0];
} }
throw new Exception("This TLD ($tld) is not supported."); throw new Exception("This TLD ($tld) is not supported");
} }
private static function getTld($domain): string private static function getTld($domain): string
{ {
$lastDotPosition = strrpos($domain, '.'); $lastDotPosition = strrpos($domain, '.');
if ($lastDotPosition === false) { if ($lastDotPosition === false) {
throw new Exception("Domain must contain at least one dot."); throw new Exception("Domain must contain at least one dot");
} }
return strtolower(substr($domain, $lastDotPosition + 1)); return strtolower(substr($domain, $lastDotPosition + 1));
} }
/**
* @throws Exception
*/
private function registerEntity(array $rdapEntity): Entity private function registerEntity(array $rdapEntity): Entity
{ {
$entity = $this->entityRepository->findOneBy([ $entity = $this->entityRepository->findOneBy([