You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2020/12/17 14:19:30 UTC

[GitHub] [iceberg] RussellSpitzer commented on a change in pull request #1909: Add ExceptionUtil.runSafely

RussellSpitzer commented on a change in pull request #1909:
URL: https://github.com/apache/iceberg/pull/1909#discussion_r545124571



##########
File path: core/src/main/java/org/apache/iceberg/util/ExceptionUtil.java
##########
@@ -36,4 +40,96 @@ private ExceptionUtil() {
     }
     throw new RuntimeException(exception);
   }
+
+  interface Block<R, E1 extends Exception, E2 extends Exception, E3 extends Exception> {
+    R run() throws E1, E2, E3;
+  }
+
+  interface CatchBlock {
+    void run(Throwable failure) throws Exception;
+  }
+
+  interface FinallyBlock {
+    void run() throws Exception;
+  }
+
+  public static <R> R runSafely(
+      Block<R, RuntimeException, RuntimeException, RuntimeException> block,
+      CatchBlock catchBlock,
+      FinallyBlock finallyBlock) {
+    return runSafely(block, catchBlock, finallyBlock,
+        RuntimeException.class, RuntimeException.class, RuntimeException.class);
+  }
+
+  public static <R, E1 extends Exception> R runSafely(
+      Block<R, E1, RuntimeException, RuntimeException> block,
+      CatchBlock catchBlock,
+      FinallyBlock finallyBlock,
+      Class<? extends E1> e1Class) throws E1 {
+    return runSafely(block, catchBlock, finallyBlock, e1Class, RuntimeException.class, RuntimeException.class);
+  }
+
+  public static <R, E1 extends Exception, E2 extends Exception> R runSafely(
+      Block<R, E1, E2, RuntimeException> block,
+      CatchBlock catchBlock,
+      FinallyBlock finallyBlock,
+      Class<? extends E1> e1Class,
+      Class<? extends E2> e2Class) throws E1, E2 {
+    return runSafely(block, catchBlock, finallyBlock, e1Class, e2Class, RuntimeException.class);
+  }
+
+  public static <R, E1 extends Exception, E2 extends Exception, E3 extends Exception> R runSafely(
+      Block<R, E1, E2, E3> block,
+      CatchBlock catchBlock,
+      FinallyBlock finallyBlock,
+      Class<? extends E1> e1Class,
+      Class<? extends E2> e2Class,
+      Class<? extends E3> e3Class) throws E1, E2, E3 {
+    Exception failure = null;
+    try {
+      return block.run();
+
+    } catch (Exception t) {
+      failure = t;
+
+      if (catchBlock != null) {
+        try {
+          catchBlock.run(failure);
+        } catch (Exception e) {
+          LOG.warn("Suppressing failure in catch block", e);
+          failure.addSuppressed(e);
+        }
+      }
+
+      tryThrowAs(failure, e1Class);
+      tryThrowAs(failure, e2Class);
+      tryThrowAs(failure, e3Class);
+      tryThrowAs(failure, RuntimeException.class);
+      throw new RuntimeException("Unknown throwable", failure);

Review comment:
       How do we get here? Is this just for safety? From what I can tell we only allow the block to have called exceptions for e1 -> e3, and all that's left should be RuntimeExceptions right?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org