From ba80fbbe05489227d64dcbd4f5e91bef68072c37 Mon Sep 17 00:00:00 2001 From: alexlehm Date: Sun, 31 Jul 2016 03:05:23 +0200 Subject: [PATCH] UnknownHostException mentions hostname with search domain added Motivation: When a hostname cannot be resolved, the message in the UnknownHostException mentions the hostname with the last attempted search domain appended, which is kind of confusing. I would prefer to see the original hostname supplied to the method in the exception. Modifications: Store the pristine hostname in the resolver context and use it to create the exception message instead of the hostname with search domain. Add unit test to check that the exception does not mention the search domain. Result: The exception mentions the unmodified hostname in the message. --- .../resolver/dns/DnsNameResolverContext.java | 11 +++++++-- .../netty/resolver/dns/SearchDomainTest.java | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolverContext.java b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolverContext.java index 08ba335a6a..3579e0644e 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolverContext.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolverContext.java @@ -69,6 +69,7 @@ abstract class DnsNameResolverContext { private final DnsNameResolver parent; private final DnsServerAddressStream nameServerAddrs; private final String hostname; + protected String pristineHostname; private final DnsCache resolveCache; private final boolean traceEnabled; private final int maxAllowedQueries; @@ -116,6 +117,7 @@ abstract class DnsNameResolverContext { String nextHostname = DnsNameResolverContext.this.hostname + "." + searchDomain; DnsNameResolverContext nextContext = newResolverContext(parent, nextHostname, resolveCache); + nextContext.pristineHostname = hostname; nextContext.internalResolve(nextPromise); nextPromise.addListener(this); } else { @@ -449,8 +451,13 @@ abstract class DnsNameResolverContext { final int tries = maxAllowedQueries - allowedQueries; final StringBuilder buf = new StringBuilder(64); - buf.append("failed to resolve '") - .append(hostname).append('\''); + buf.append("failed to resolve '"); + if (pristineHostname != null) { + buf.append(pristineHostname); + } else { + buf.append(hostname); + } + buf.append('\''); if (tries > 1) { if (tries < maxAllowedQueries) { buf.append(" after ") diff --git a/resolver-dns/src/test/java/io/netty/resolver/dns/SearchDomainTest.java b/resolver-dns/src/test/java/io/netty/resolver/dns/SearchDomainTest.java index da89173848..77c652ff79 100644 --- a/resolver-dns/src/test/java/io/netty/resolver/dns/SearchDomainTest.java +++ b/resolver-dns/src/test/java/io/netty/resolver/dns/SearchDomainTest.java @@ -24,6 +24,7 @@ import org.junit.Before; import org.junit.Test; import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -34,7 +35,10 @@ import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.core.StringContains.containsString; public class SearchDomainTest { @@ -265,4 +269,23 @@ public class SearchDomainTest { } return list; } + + @Test + public void testExceptionMsgNoSearchDomain() throws Exception { + Set domains = new HashSet(); + + TestDnsServer.MapRecordStoreA store = new TestDnsServer.MapRecordStoreA(domains); + dnsServer = new TestDnsServer(store); + dnsServer.start(); + + resolver = newResolver().searchDomains(Collections.singletonList("foo.com")).build(); + + Future fut = resolver.resolve("unknown.hostname"); + assertTrue(fut.await(10, TimeUnit.SECONDS)); + assertFalse(fut.isSuccess()); + final Throwable cause = fut.cause(); + assertEquals(UnknownHostException.class, cause.getClass()); + assertThat("search domain is included in UnknownHostException", cause.getMessage(), + not(containsString("foo.com"))); + } }