浏览代码

HADOOP-16453. Update how exceptions are handled in NetUtils. Contributed by Lisheng Sun.

Inigo Goiri 6 年之前
父节点
当前提交
6b4564f1d5

+ 7 - 3
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java

@@ -804,7 +804,11 @@ public class NetUtils {
                 + ";"
                 + see("SocketException"));
       } else {
-        // Return instance of same type if Exception has a String constructor
+        // 1. Return instance of same type with exception msg if Exception has a
+        // String constructor.
+        // 2. Return instance of same type if Exception doesn't have a String
+        // constructor.
+        // Related HADOOP-16453.
         return wrapWithMessage(exception,
             "DestHost:destPort " + destHost + ":" + destPort
                 + " , LocalHost:localPort " + localHost
@@ -832,9 +836,9 @@ public class NetUtils {
       Constructor<? extends Throwable> ctor = clazz.getConstructor(String.class);
       Throwable t = ctor.newInstance(msg);
       return (T)(t.initCause(exception));
+    } catch (NoSuchMethodException e) {
+      return exception;
     } catch (Throwable e) {
-      LOG.trace("Unable to wrap exception of type {}: it has no (String) "
-          + "constructor", clazz, e);
       throw exception;
     }
   }

+ 3 - 4
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java

@@ -1582,11 +1582,10 @@ public class TestIPC {
     try {
       call(client, 0, addr, conf);
     } catch (IOException ioe) {
-      Throwable t = ioe.getCause();
-      Assert.assertNotNull(t);
-      Assert.assertEquals(RpcException.class, t.getClass());
+      Assert.assertNotNull(ioe);
+      Assert.assertEquals(RpcException.class, ioe.getClass());
       Assert.assertEquals("RPC response exceeds maximum data length",
-          t.getMessage());
+          ioe.getMessage());
       return;
     }
     Assert.fail("didn't get limit exceeded");

+ 5 - 10
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java

@@ -279,11 +279,9 @@ public class TestNetUtils {
   @Test
   public void testWrapIOEWithNoStringConstructor() throws Throwable {
     IOException e = new CharacterCodingException();
-    IOException wrapped = verifyExceptionClass(e, IOException.class);
-    assertInException(wrapped, "Failed on local exception");
-    assertNotInException(wrapped, NetUtils.HADOOP_WIKI);
-    assertInException(wrapped, "Host Details ");
-    assertRemoteDetailsIncluded(wrapped);
+    IOException wrapped =
+        verifyExceptionClass(e, CharacterCodingException.class);
+    assertEquals(null, wrapped.getMessage());
   }
 
   @Test
@@ -295,11 +293,8 @@ public class TestNetUtils {
       }
     }
     IOException e = new TestIOException();
-    IOException wrapped = verifyExceptionClass(e, IOException.class);
-    assertInException(wrapped, "Failed on local exception");
-    assertNotInException(wrapped, NetUtils.HADOOP_WIKI);
-    assertInException(wrapped, "Host Details ");
-    assertRemoteDetailsIncluded(wrapped);
+    IOException wrapped = verifyExceptionClass(e, TestIOException.class);
+    assertEquals(null, wrapped.getMessage());
   }
 
   @Test