Skip to content

Commit 4e70843

Browse files
committed
Omit internal hostname argument from error messages
1 parent 2c16a9c commit 4e70843

File tree

4 files changed

+94
-14
lines changed

4 files changed

+94
-14
lines changed

src/DnsConnector.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,23 @@ public function connect($uri)
4444
return new Promise\Promise(
4545
function ($resolve, $reject) use (&$promise, &$resolved, $uri, $connector, $host, $parts) {
4646
// resolve/reject with result of DNS lookup
47-
$promise->then(function ($ip) use (&$promise, &$resolved, $connector, $host, $parts) {
47+
$promise->then(function ($ip) use (&$promise, &$resolved, $uri, $connector, $host, $parts) {
4848
$resolved = $ip;
49-
$uri = Connector::uri($parts, $host, $ip);
5049

51-
return $promise = $connector->connect($uri);
50+
return $promise = $connector->connect(
51+
Connector::uri($parts, $host, $ip)
52+
)->then(null, function (\Exception $e) use ($uri) {
53+
if ($e instanceof \RuntimeException) {
54+
$message = \preg_replace('/^(Connection to [^ ]+)[&?]hostname=[^ &]+/', '$1', $e->getMessage());
55+
$e = new \RuntimeException(
56+
'Connection to ' . $uri . ' failed: ' . $message,
57+
$e->getCode(),
58+
$e
59+
);
60+
}
61+
62+
throw $e;
63+
});
5264
}, function ($e) use ($uri, $reject) {
5365
$reject(new \RuntimeException('Connection to ' . $uri .' failed during DNS lookup: ' . $e->getMessage(), 0, $e));
5466
})->then($resolve, $reject);

src/HappyEyeBallsConnectionBuilder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,12 @@ public function check($resolve, $reject)
175175

176176
$that->failureCount++;
177177

178+
$message = \preg_replace('/^(Connection to [^ ]+)[&?]hostname=[^ &]+/', '$1', $e->getMessage());
178179
if (\strpos($ip, ':') === false) {
179-
$that->lastError4 = $e->getMessage();
180+
$that->lastError4 = $message;
180181
$that->lastErrorFamily = 4;
181182
} else {
182-
$that->lastError6 = $e->getMessage();
183+
$that->lastError6 = $message;
183184
$that->lastErrorFamily = 6;
184185
}
185186

tests/DnsConnectorTest.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,29 +81,55 @@ public function testRejectsImmediatelyIfUriIsInvalid()
8181
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
8282
}
8383

84-
public function testRejectsWithTcpConnectorRejectionIfGivenIp()
84+
public function testConnectRejectsIfGivenIpAndTcpConnectorRejectsWithRuntimeException()
8585
{
86-
$promise = Promise\reject(new \RuntimeException('Connection failed'));
86+
$promise = Promise\reject(new \RuntimeException('Connection to tcp://1.2.3.4:80 failed: Connection failed', 42));
8787
$this->resolver->expects($this->never())->method('resolve');
88-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80'))->willReturn($promise);
88+
$this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80')->willReturn($promise);
8989

9090
$promise = $this->connector->connect('1.2.3.4:80');
9191
$promise->cancel();
9292

93-
$this->setExpectedException('RuntimeException', 'Connection failed');
93+
$this->setExpectedException('RuntimeException', 'Connection to tcp://1.2.3.4:80 failed: Connection failed', 42);
9494
$this->throwRejection($promise);
9595
}
9696

97-
public function testRejectsWithTcpConnectorRejectionAfterDnsIsResolved()
97+
public function testConnectRejectsIfGivenIpAndTcpConnectorRejectsWithInvalidArgumentException()
9898
{
99-
$promise = Promise\reject(new \RuntimeException('Connection failed'));
100-
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.com'))->willReturn(Promise\resolve('1.2.3.4'));
101-
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($promise);
99+
$promise = Promise\reject(new \InvalidArgumentException('Invalid', 42));
100+
$this->resolver->expects($this->never())->method('resolve');
101+
$this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80')->willReturn($promise);
102+
103+
$promise = $this->connector->connect('1.2.3.4:80');
104+
$promise->cancel();
105+
106+
$this->setExpectedException('InvalidArgumentException', 'Invalid', 42);
107+
$this->throwRejection($promise);
108+
}
109+
110+
public function testConnectRejectsWithOriginalHostnameInMessageAfterResolvingIfTcpConnectorRejectsWithRuntimeException()
111+
{
112+
$promise = Promise\reject(new \RuntimeException('Connection to tcp://1.2.3.4:80?hostname=example.com failed: Connection failed', 42));
113+
$this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\resolve('1.2.3.4'));
114+
$this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn($promise);
115+
116+
$promise = $this->connector->connect('example.com:80');
117+
$promise->cancel();
118+
119+
$this->setExpectedException('RuntimeException', 'Connection to example.com:80 failed: Connection to tcp://1.2.3.4:80 failed: Connection failed', 42);
120+
$this->throwRejection($promise);
121+
}
122+
123+
public function testConnectRejectsWithOriginalExceptionAfterResolvingIfTcpConnectorRejectsWithInvalidArgumentException()
124+
{
125+
$promise = Promise\reject(new \InvalidArgumentException('Invalid', 42));
126+
$this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\resolve('1.2.3.4'));
127+
$this->tcp->expects($this->once())->method('connect')->with('1.2.3.4:80?hostname=example.com')->willReturn($promise);
102128

103129
$promise = $this->connector->connect('example.com:80');
104130
$promise->cancel();
105131

106-
$this->setExpectedException('RuntimeException', 'Connection failed');
132+
$this->setExpectedException('InvalidArgumentException', 'Invalid', 42);
107133
$this->throwRejection($promise);
108134
}
109135

tests/HappyEyeBallsConnectionBuilderTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,47 @@ public function testConnectWillRejectWhenAllConnectionsRejectAndCancelNextAttemp
553553
$this->assertEquals('Connection to tcp://reactphp.org:80 failed: Connection refused', $exception->getMessage());
554554
}
555555

556+
public function testConnectWillRejectWithMessageWithoutHostnameWhenAllConnectionsRejectAndCancelNextAttemptTimerImmediately()
557+
{
558+
$timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock();
559+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
560+
$loop->expects($this->once())->method('addTimer')->with(0.1, $this->anything())->willReturn($timer);
561+
$loop->expects($this->once())->method('cancelTimer')->with($timer);
562+
563+
$deferred = new Deferred();
564+
$connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock();
565+
$connector->expects($this->exactly(2))->method('connect')->willReturnOnConsecutiveCalls(
566+
$deferred->promise(),
567+
\React\Promise\reject(new \RuntimeException('Connection to tcp://127.0.0.1:80?hostname=localhost failed: Connection refused'))
568+
);
569+
570+
$resolver = $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
571+
$resolver->expects($this->exactly(2))->method('resolveAll')->withConsecutive(
572+
array('localhost', Message::TYPE_AAAA),
573+
array('localhost', Message::TYPE_A)
574+
)->willReturnOnConsecutiveCalls(
575+
\React\Promise\resolve(array('::1')),
576+
\React\Promise\resolve(array('127.0.0.1'))
577+
);
578+
579+
$uri = 'tcp://localhost:80';
580+
$host = 'localhost';
581+
$parts = parse_url($uri);
582+
583+
$builder = new HappyEyeBallsConnectionBuilder($loop, $connector, $resolver, $uri, $host, $parts);
584+
585+
$promise = $builder->connect();
586+
$deferred->reject(new \RuntimeException('Connection to tcp://[::1]:80?hostname=localhost failed: Connection refused'));
587+
588+
$exception = null;
589+
$promise->then(null, function ($e) use (&$exception) {
590+
$exception = $e;
591+
});
592+
593+
$this->assertInstanceOf('RuntimeException', $exception);
594+
$this->assertEquals('Connection to tcp://localhost:80 failed: Last error for IPv4: Connection to tcp://127.0.0.1:80 failed: Connection refused. Previous error for IPv6: Connection to tcp://[::1]:80 failed: Connection refused', $exception->getMessage());
595+
}
596+
556597
public function testCancelConnectWillRejectPromiseAndCancelBothDnsLookups()
557598
{
558599
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

0 commit comments

Comments
 (0)