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.
This commit is contained in:
parent
1975fcefe4
commit
ba80fbbe05
@ -69,6 +69,7 @@ abstract class DnsNameResolverContext<T> {
|
||||
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<T> {
|
||||
String nextHostname = DnsNameResolverContext.this.hostname + "." + searchDomain;
|
||||
DnsNameResolverContext<T> nextContext = newResolverContext(parent,
|
||||
nextHostname, resolveCache);
|
||||
nextContext.pristineHostname = hostname;
|
||||
nextContext.internalResolve(nextPromise);
|
||||
nextPromise.addListener(this);
|
||||
} else {
|
||||
@ -449,8 +451,13 @@ abstract class DnsNameResolverContext<T> {
|
||||
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 ")
|
||||
|
@ -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<String> domains = new HashSet<String>();
|
||||
|
||||
TestDnsServer.MapRecordStoreA store = new TestDnsServer.MapRecordStoreA(domains);
|
||||
dnsServer = new TestDnsServer(store);
|
||||
dnsServer.start();
|
||||
|
||||
resolver = newResolver().searchDomains(Collections.singletonList("foo.com")).build();
|
||||
|
||||
Future<InetAddress> 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")));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user