Skip to content

Commit

Permalink
simplify error handling for dns_get_record()
Browse files Browse the repository at this point in the history
  • Loading branch information
aivchen committed Oct 15, 2023
1 parent ebaaf5b commit 2978d4c
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions src/Validation/DNSGetRecordWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,12 @@ class DNSGetRecordWrapper
/**
* @param string $host
* @param int $type
*
*
* @return DNSRecords
*/
public function getRecords(string $host, int $type): DNSRecords
{
// A workaround to fix https://bugs.php.net/bug.php?id=73149
/** @psalm-suppress InvalidArgument */
set_error_handler(
static function (int $errorLevel, string $errorMessage): never {
throw new \RuntimeException("Unable to get DNS record for the host: $errorMessage");
}
);
try {
// Get all MX, A and AAAA DNS records for host
return new DNSRecords(dns_get_record($host, $type));
} catch (\RuntimeException $exception) {
return new DNSRecords([], true);
} finally {
restore_error_handler();
}
$result = @dns_get_record($host, $type);
return new DNSRecords($result === false ? [] : $result);
}
}

0 comments on commit 2978d4c

Please sign in to comment.