19
19
use const DNS_AAAA ;
20
20
use function dns_get_record ;
21
21
use Laudis \Neo4j \Contracts \AddressResolverInterface ;
22
+ use Throwable ;
22
23
23
24
class DNSAddressResolver implements AddressResolverInterface
24
25
{
@@ -31,8 +32,13 @@ public function getAddresses(string $host): iterable
31
32
// as late as possible
32
33
yield $ host ;
33
34
34
- /** @var list<array{ip?: string|null}> $records */
35
- $ records = dns_get_record ($ host , DNS_A | DNS_AAAA );
35
+ try {
36
+ /** @var list<array{ip?: string|null}> $records */
37
+ $ records = dns_get_record ($ host , DNS_A | DNS_AAAA );
38
+ } catch (Throwable $ e ) {
39
+ $ records = []; // Failed DNS queries should not halt execution
40
+ }
41
+
36
42
if (count ($ records ) === 0 ) {
37
43
yield from $ this ->tryReverseLookup ($ host );
38
44
} else {
@@ -47,8 +53,13 @@ public function getAddresses(string $host): iterable
47
53
*/
48
54
private function tryReverseLookup (string $ host ): iterable
49
55
{
50
- /** @var list<array{target?: string|null}> $records */
51
- $ records = dns_get_record ($ host .'.in-addr.arpa ' );
56
+ try {
57
+ /** @var list<array{target?: string|null}> $records */
58
+ $ records = dns_get_record ($ host .'.in-addr.arpa ' );
59
+ } catch (Throwable $ e ) {
60
+ $ records = []; // Failed DNS queries should not halt execution
61
+ }
62
+
52
63
if (count ($ records ) !== 0 ) {
53
64
$ records = array_map (static fn (array $ x ) => $ x ['target ' ] ?? '' , $ records );
54
65
$ records = array_filter ($ records , static fn (string $ x ) => $ x !== '' );
0 commit comments