|
@@ -715,9 +715,9 @@ public class NetUtils {
|
|
|
* return an IOException with the input exception as the cause and also
|
|
|
* include the host details. The new exception provides the stack trace of the
|
|
|
* place where the exception is thrown and some extra diagnostics information.
|
|
|
- * If the exception is BindException or ConnectException or
|
|
|
- * UnknownHostException or SocketTimeoutException, return a new one of the
|
|
|
- * same type; Otherwise return an IOException.
|
|
|
+ * If the exception is of type BindException, ConnectException,
|
|
|
+ * UnknownHostException, SocketTimeoutException or has a String constructor,
|
|
|
+ * return a new one of the same type; Otherwise return an IOException.
|
|
|
*
|
|
|
* @param destHost target host (nullable)
|
|
|
* @param destPort target port
|
|
@@ -731,83 +731,90 @@ public class NetUtils {
|
|
|
final String localHost,
|
|
|
final int localPort,
|
|
|
final IOException exception) {
|
|
|
- if (exception instanceof BindException) {
|
|
|
- return wrapWithMessage(exception,
|
|
|
- "Problem binding to ["
|
|
|
- + localHost
|
|
|
- + ":"
|
|
|
- + localPort
|
|
|
- + "] "
|
|
|
- + exception
|
|
|
- + ";"
|
|
|
- + see("BindException"));
|
|
|
- } else if (exception instanceof ConnectException) {
|
|
|
- // Check if client was trying to connect to an unspecified IPv4 address
|
|
|
- // (0.0.0.0) or IPv6 address(0:0:0:0:0:0:0:0 or ::)
|
|
|
- if ((destHost != null && (destHost.equals("0.0.0.0") ||
|
|
|
- destHost.equals("0:0:0:0:0:0:0:0") || destHost.equals("::")))
|
|
|
- || destPort == 0) {
|
|
|
- return wrapWithMessage(exception, "Your endpoint configuration" +
|
|
|
- " is wrong;" + see("UnsetHostnameOrPort"));
|
|
|
- } else {
|
|
|
- // connection refused; include the host:port in the error
|
|
|
+ try {
|
|
|
+ if (exception instanceof BindException) {
|
|
|
return wrapWithMessage(exception,
|
|
|
- "Call From "
|
|
|
+ "Problem binding to ["
|
|
|
+ localHost
|
|
|
- + " to "
|
|
|
- + destHost
|
|
|
+ ":"
|
|
|
- + destPort
|
|
|
- + " failed on connection exception: "
|
|
|
+ + localPort
|
|
|
+ + "] "
|
|
|
+ exception
|
|
|
+ ";"
|
|
|
- + see("ConnectionRefused"));
|
|
|
+ + see("BindException"));
|
|
|
+ } else if (exception instanceof ConnectException) {
|
|
|
+ // Check if client was trying to connect to an unspecified IPv4 address
|
|
|
+ // (0.0.0.0) or IPv6 address(0:0:0:0:0:0:0:0 or ::)
|
|
|
+ if ((destHost != null && (destHost.equals("0.0.0.0") ||
|
|
|
+ destHost.equals("0:0:0:0:0:0:0:0") || destHost.equals("::")))
|
|
|
+ || destPort == 0) {
|
|
|
+ return wrapWithMessage(exception, "Your endpoint configuration" +
|
|
|
+ " is wrong;" + see("UnsetHostnameOrPort"));
|
|
|
+ } else {
|
|
|
+ // connection refused; include the host:port in the error
|
|
|
+ return wrapWithMessage(exception,
|
|
|
+ "Call From "
|
|
|
+ + localHost
|
|
|
+ + " to "
|
|
|
+ + destHost
|
|
|
+ + ":"
|
|
|
+ + destPort
|
|
|
+ + " failed on connection exception: "
|
|
|
+ + exception
|
|
|
+ + ";"
|
|
|
+ + see("ConnectionRefused"));
|
|
|
+ }
|
|
|
+ } else if (exception instanceof UnknownHostException) {
|
|
|
+ return wrapWithMessage(exception,
|
|
|
+ "Invalid host name: "
|
|
|
+ + getHostDetailsAsString(destHost, destPort, localHost)
|
|
|
+ + exception
|
|
|
+ + ";"
|
|
|
+ + see("UnknownHost"));
|
|
|
+ } else if (exception instanceof SocketTimeoutException) {
|
|
|
+ return wrapWithMessage(exception,
|
|
|
+ "Call From "
|
|
|
+ + localHost + " to " + destHost + ":" + destPort
|
|
|
+ + " failed on socket timeout exception: " + exception
|
|
|
+ + ";"
|
|
|
+ + see("SocketTimeout"));
|
|
|
+ } else if (exception instanceof NoRouteToHostException) {
|
|
|
+ return wrapWithMessage(exception,
|
|
|
+ "No Route to Host from "
|
|
|
+ + localHost + " to " + destHost + ":" + destPort
|
|
|
+ + " failed on socket timeout exception: " + exception
|
|
|
+ + ";"
|
|
|
+ + see("NoRouteToHost"));
|
|
|
+ } else if (exception instanceof EOFException) {
|
|
|
+ return wrapWithMessage(exception,
|
|
|
+ "End of File Exception between "
|
|
|
+ + getHostDetailsAsString(destHost, destPort, localHost)
|
|
|
+ + ": " + exception
|
|
|
+ + ";"
|
|
|
+ + see("EOFException"));
|
|
|
+ } else if (exception instanceof SocketException) {
|
|
|
+ // Many of the predecessor exceptions are subclasses of SocketException,
|
|
|
+ // so must be handled before this
|
|
|
+ return wrapWithMessage(exception,
|
|
|
+ "Call From "
|
|
|
+ + localHost + " to " + destHost + ":" + destPort
|
|
|
+ + " failed on socket exception: " + exception
|
|
|
+ + ";"
|
|
|
+ + see("SocketException"));
|
|
|
+ } else {
|
|
|
+ // Return instance of same type if Exception has a String constructor
|
|
|
+ return wrapWithMessage(exception,
|
|
|
+ "DestHost:destPort " + destHost + ":" + destPort
|
|
|
+ + " , LocalHost:localPort " + localHost
|
|
|
+ + ":" + localPort + ". Failed on local exception: " +
|
|
|
+ exception);
|
|
|
+
|
|
|
}
|
|
|
- } else if (exception instanceof UnknownHostException) {
|
|
|
- return wrapWithMessage(exception,
|
|
|
- "Invalid host name: "
|
|
|
- + getHostDetailsAsString(destHost, destPort, localHost)
|
|
|
- + exception
|
|
|
- + ";"
|
|
|
- + see("UnknownHost"));
|
|
|
- } else if (exception instanceof SocketTimeoutException) {
|
|
|
- return wrapWithMessage(exception,
|
|
|
- "Call From "
|
|
|
- + localHost + " to " + destHost + ":" + destPort
|
|
|
- + " failed on socket timeout exception: " + exception
|
|
|
- + ";"
|
|
|
- + see("SocketTimeout"));
|
|
|
- } else if (exception instanceof NoRouteToHostException) {
|
|
|
- return wrapWithMessage(exception,
|
|
|
- "No Route to Host from "
|
|
|
- + localHost + " to " + destHost + ":" + destPort
|
|
|
- + " failed on socket timeout exception: " + exception
|
|
|
- + ";"
|
|
|
- + see("NoRouteToHost"));
|
|
|
- } else if (exception instanceof EOFException) {
|
|
|
- return wrapWithMessage(exception,
|
|
|
- "End of File Exception between "
|
|
|
- + getHostDetailsAsString(destHost, destPort, localHost)
|
|
|
- + ": " + exception
|
|
|
- + ";"
|
|
|
- + see("EOFException"));
|
|
|
- } else if (exception instanceof SocketException) {
|
|
|
- // Many of the predecessor exceptions are subclasses of SocketException,
|
|
|
- // so must be handled before this
|
|
|
- return wrapWithMessage(exception,
|
|
|
- "Call From "
|
|
|
- + localHost + " to " + destHost + ":" + destPort
|
|
|
- + " failed on socket exception: " + exception
|
|
|
- + ";"
|
|
|
- + see("SocketException"));
|
|
|
- }
|
|
|
- else {
|
|
|
+ } catch (IOException ex) {
|
|
|
return (IOException) new IOException("Failed on local exception: "
|
|
|
- + exception
|
|
|
- + "; Host Details : "
|
|
|
- + getHostDetailsAsString(destHost, destPort, localHost))
|
|
|
+ + exception + "; Host Details : "
|
|
|
+ + getHostDetailsAsString(destHost, destPort, localHost))
|
|
|
.initCause(exception);
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -817,16 +824,16 @@ public class NetUtils {
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
private static <T extends IOException> T wrapWithMessage(
|
|
|
- T exception, String msg) {
|
|
|
+ T exception, String msg) throws T {
|
|
|
Class<? extends Throwable> clazz = exception.getClass();
|
|
|
try {
|
|
|
Constructor<? extends Throwable> ctor = clazz.getConstructor(String.class);
|
|
|
Throwable t = ctor.newInstance(msg);
|
|
|
return (T)(t.initCause(exception));
|
|
|
} catch (Throwable e) {
|
|
|
- LOG.warn("Unable to wrap exception of type " +
|
|
|
- clazz + ": it has no (String) constructor", e);
|
|
|
- return exception;
|
|
|
+ LOG.warn("Unable to wrap exception of type {}: it has no (String) "
|
|
|
+ + "constructor", clazz, e);
|
|
|
+ throw exception;
|
|
|
}
|
|
|
}
|
|
|
|