1
0
mirror of https://github.com/danog/dns.git synced 2024-11-30 04:29:06 +01:00

Fix type error if config loading results in exception (#109)

This commit is contained in:
Niklas Keller 2023-01-21 16:54:40 +01:00 committed by GitHub
parent 12884f8d9c
commit f528711062
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 27 deletions

View File

@ -4,5 +4,8 @@ namespace Amp\Dns;
interface DnsConfigLoader
{
/**
* @throws DnsConfigException
*/
public function loadConfig(): DnsConfig;
}

View File

@ -100,10 +100,7 @@ final class Rfc1035StubDnsResolver implements DnsResolver
default => throw new \Error("Invalid value for parameter 2: null|Record::A|Record::AAAA expected"),
};
if ($this->configStatus === self::CONFIG_NOT_LOADED) {
$this->reloadConfig();
}
$this->loadConfigIfNotLoaded();
if ($this->configStatus === self::CONFIG_FAILED) {
return $this->blockingFallbackResolver->resolve($name, $typeRestriction, $cancellation);
}
@ -218,6 +215,8 @@ final class Rfc1035StubDnsResolver implements DnsResolver
* Reloads the configuration in the background.
*
* Once it's finished, the configuration will be used for new requests.
*
* @throws DnsConfigException
*/
public function reloadConfig(): DnsConfig
{
@ -232,29 +231,11 @@ final class Rfc1035StubDnsResolver implements DnsResolver
} catch (DnsConfigException $e) {
$this->configStatus = self::CONFIG_FAILED;
$message = "Could not load the system's DNS configuration; "
. "falling back to synchronous, blocking resolver; "
. \get_class($e) . ": " . $e->getMessage();
try {
\trigger_error(
$message,
\E_USER_WARNING
);
} catch (\Throwable) {
\set_error_handler(null);
\trigger_error(
$message,
\E_USER_WARNING
);
\restore_error_handler();
}
throw $e;
} finally {
$this->pendingConfig = null;
}
\assert($this->config !== null);
return $this->config;
});
@ -271,10 +252,7 @@ final class Rfc1035StubDnsResolver implements DnsResolver
$future = async(function () use ($name, $type, $cancellation): array {
try {
if ($this->configStatus === self::CONFIG_NOT_LOADED) {
$this->reloadConfig();
}
$this->loadConfigIfNotLoaded();
if ($this->configStatus === self::CONFIG_FAILED) {
return $this->blockingFallbackResolver->query($name, $type, $cancellation);
}
@ -558,4 +536,33 @@ final class Rfc1035StubDnsResolver implements DnsResolver
MessageResponseCodes::NAME_ERROR,
], true);
}
private function loadConfigIfNotLoaded(): void
{
if ($this->configStatus !== self::CONFIG_NOT_LOADED) {
return;
}
try {
$this->reloadConfig();
} catch (DnsConfigException $e) {
$message = "Could not load the system's DNS configuration; "
. "falling back to synchronous, blocking resolver; "
. \get_class($e) . ": " . $e->getMessage();
try {
\trigger_error(
$message,
\E_USER_WARNING
);
} catch (\Throwable) {
\set_error_handler(null);
\trigger_error(
$message,
\E_USER_WARNING
);
\restore_error_handler();
}
}
}
}