From b8034d9c095bc030a63281fe59381aa2bf299cbe Mon Sep 17 00:00:00 2001 From: Michel Bardelmeijer Date: Mon, 30 Oct 2023 13:50:48 +0100 Subject: [PATCH] Handle invalid RDAP response --- README.md | 6 +++++- src/Exceptions/InvalidRdapResponse.php | 16 ++++++++++++++++ src/Exceptions/RdapException.php | 8 ++++++++ src/Exceptions/RdapRequestTimedOut.php | 4 ++-- src/Rdap.php | 7 +++++++ tests/RdapTest.php | 17 +++++++++++++++++ 6 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 src/Exceptions/InvalidRdapResponse.php create mode 100644 src/Exceptions/RdapException.php diff --git a/README.md b/README.md index e2a3960..14661a6 100644 --- a/README.md +++ b/README.md @@ -165,10 +165,14 @@ $domain = Rdap::domain('google.com'); // returns an instance of `Spatie\Rdap\Res If you pass a non-existing domain, then the `domain()` function will return `null`. -## Handling timeouts +## Handling errors Sometimes RDAP is slow in responding. If a response isn't returned in a timely manner, a `Spatie\Rdap\Exceptions\RdapRequestTimedOut` exception will be thrown. +Sometimes RDAP servers return with an invalid response. If that happens, a `Spatie\Rdap\Exceptions\InvalidRdapResponse` exception will be thrown. + +Both exceptions implement `Spatie\Rdap\Exceptions\RdapException`. You can catch that exception to handle both cases. + ## Working with RDAP DNS For each TLD a specific server is used to respond to domain queries. Such a server is called a "DNS server". The official list of all RDAP DNS server is available as JSON [here](https://data.iana.org/rdap/dns.json). diff --git a/src/Exceptions/InvalidRdapResponse.php b/src/Exceptions/InvalidRdapResponse.php new file mode 100644 index 0000000..406a268 --- /dev/null +++ b/src/Exceptions/InvalidRdapResponse.php @@ -0,0 +1,16 @@ +toBeInstanceOf(RdapRequestTimedOut::class); } }); + +it('throws a invalid response exception if rdap servers returns invalid response', function () { + Http::fake([ + 'rdap.nic.io/*' => Http::response('invalid response'), + ]); + + try { + $result = $this->rdap->domain('invalid-domain-response.com'); + + // sometimes it returns null + expect($result)->toBeNull(); + } catch (InvalidRdapResponse $invalidResponse) { + expect($invalidResponse)->toBeInstanceOf(InvalidRdapResponse::class); + } +});