Skip to content

Commit 97076ba

Browse files
committed
Improve DNS error messages
1 parent 476e264 commit 97076ba

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/DnsConnector.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function connect($uri)
4141
}
4242

4343
return $this
44-
->resolveHostname($host)
44+
->resolveHostname($host, $uri)
4545
->then(function ($ip) use ($connector, $host, $parts) {
4646
$uri = '';
4747

@@ -89,18 +89,20 @@ public function connect($uri)
8989
});
9090
}
9191

92-
private function resolveHostname($host)
92+
private function resolveHostname($host, $uri)
9393
{
9494
$promise = $this->resolver->resolve($host);
9595

9696
return new Promise\Promise(
97-
function ($resolve, $reject) use ($promise) {
97+
function ($resolve, $reject) use ($promise, $uri) {
9898
// resolve/reject with result of DNS lookup
99-
$promise->then($resolve, $reject);
99+
$promise->then($resolve, function ($e) use ($uri, $reject) {
100+
$reject(new RuntimeException('Connection to ' . $uri .' failed during DNS lookup: ' . $e->getMessage(), 0, $e));
101+
});
100102
},
101-
function ($_, $reject) use ($promise) {
103+
function ($_, $reject) use ($promise, $uri) {
102104
// cancellation should reject connection attempt
103-
$reject(new RuntimeException('Connection attempt cancelled during DNS lookup'));
105+
$reject(new RuntimeException('Connection to ' . $uri . ' cancelled during DNS lookup'));
104106

105107
// (try to) cancel pending DNS lookup
106108
if ($promise instanceof CancellablePromiseInterface) {

tests/DnsConnectorTest.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,38 @@ public function testRejectsImmediatelyIfUriIsInvalid()
7777
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
7878
}
7979

80+
/**
81+
* @expectedException RuntimeException
82+
* @expectedExceptionMessage Connection to example.invalid:80 failed during DNS lookup: DNS error
83+
*/
8084
public function testSkipConnectionIfDnsFails()
8185
{
82-
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.invalid'))->will($this->returnValue(Promise\reject()));
86+
$promise = Promise\reject(new \RuntimeException('DNS error'));
87+
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.invalid'))->willReturn($promise);
8388
$this->tcp->expects($this->never())->method('connect');
8489

85-
$this->connector->connect('example.invalid:80');
90+
$promise = $this->connector->connect('example.invalid:80');
91+
92+
$this->throwRejection($promise);
93+
}
94+
95+
public function testRejectionExceptionUsesPreviousExceptionIfDnsFails()
96+
{
97+
$exception = new \RuntimeException();
98+
99+
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.invalid'))->willReturn(Promise\reject($exception));
100+
101+
$promise = $this->connector->connect('example.invalid:80');
102+
103+
$promise->then(null, function ($e) {
104+
throw $e->getPrevious();
105+
})->then(null, $this->expectCallableOnceWith($this->identicalTo($exception)));
86106
}
87107

108+
/**
109+
* @expectedException RuntimeException
110+
* @expectedExceptionMessage Connection to example.com:80 cancelled during DNS lookup
111+
*/
88112
public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
89113
{
90114
$pending = new Promise\Promise(function () { }, $this->expectCallableOnce());
@@ -94,7 +118,7 @@ public function testCancelDuringDnsCancelsDnsAndDoesNotStartTcpConnection()
94118
$promise = $this->connector->connect('example.com:80');
95119
$promise->cancel();
96120

97-
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
121+
$this->throwRejection($promise);
98122
}
99123

100124
public function testCancelDuringTcpConnectionCancelsTcpConnection()
@@ -108,4 +132,14 @@ public function testCancelDuringTcpConnectionCancelsTcpConnection()
108132

109133
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
110134
}
135+
136+
private function throwRejection($promise)
137+
{
138+
$ex = null;
139+
$promise->then(null, function ($e) use (&$ex) {
140+
$ex = $e;
141+
});
142+
143+
throw $ex;
144+
}
111145
}

0 commit comments

Comments
 (0)