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

Consolidate error handling

This commit is contained in:
Aaron Piotrowski 2017-06-23 16:25:50 -05:00
parent 8271989335
commit 6a2e666a80
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB

View File

@ -75,18 +75,7 @@ abstract class Server {
$this->lastActivity = \time();
if ($exception) {
if (!$exception instanceof ResolutionException) {
$message = "Unexpected error during resolution: " . $exception->getMessage();
$exception = new ResolutionException($message, 0, $exception);
}
$questions = $this->questions;
$this->questions = [];
foreach ($questions as $deferred) {
$deferred->fail($exception);
}
$this->error($exception);
return;
}
@ -137,6 +126,7 @@ abstract class Server {
try {
yield $this->send($message);
} catch (StreamException $exception) {
$this->error($exception);
throw new ResolutionException("Sending the request failed", 0, $exception);
}
@ -148,8 +138,9 @@ abstract class Server {
try {
return yield Promise\timeout($deferred->promise(), $timeout);
} catch (Amp\TimeoutException $e) {
} catch (Amp\TimeoutException $exception) {
unset($this->questions[$id]);
$this->error($exception);
throw new TimeoutException("Didn't receive a response within {$timeout} milliseconds.");
}
});
@ -167,6 +158,26 @@ abstract class Server {
$this->output = null;
}
private function error(\Throwable $exception) {
$this->close();
if (empty($this->questions)) {
return;
}
if (!$exception instanceof ResolutionException) {
$message = "Unexpected error during resolution: " . $exception->getMessage();
$exception = new ResolutionException($message, 0, $exception);
}
$questions = $this->questions;
$this->questions = [];
foreach ($questions as $deferred) {
$deferred->fail($exception);
}
}
protected function read(): Promise {
return $this->input->read();
}