package edu.mit.csail.db.ml.util;

import edu.mit.csail.db.ml.server.storage.ExperimentRunDao;
import modeldb.ServerLogicException;
import org.apache.thrift.TException;
import org.jooq.DSLContext;

public class ExceptionWrapper {
  public interface CheckedSupplier<T> {
    T get() throws Exception;
  }

  public interface CheckedRunnable {
    void run() throws Exception;
  }

  public static <T> T run(CheckedSupplier<T> fn) throws TException {
    try {
      return fn.get();
    } catch (Exception ex) {
      ex.printStackTrace();
      if (ex instanceof TException) {
        throw (TException) ex;
      } else {
        throw new ServerLogicException(ex.getClass().getSimpleName() + ": " + ex.getMessage());
      }
    }
  }

  public static <T> T run(int expRunId, DSLContext ctx, CheckedSupplier<T> fn) throws TException {
    return run(() -> {
      ExperimentRunDao.validateExperimentRunId(expRunId, ctx);
      return fn.get();
    });
  }
}

@@ -195,189 +195,1294 @@
 t;%0A%0A
-public class ExceptionWrapper %7B%0A  public interface CheckedSupplier%3CT%3E %7B%0A    T get() throws Exception;%0A  %7D%0A%0A  public interface CheckedRunnable %7B%0A    void run() throws Exception;%0A  %7D%0A
+/**%0A * This class contains logic for running code and wrapping any thrown exceptions.%0A */%0Apublic class ExceptionWrapper %7B%0A  /**%0A   * An interface for a function that takes no arguments, produce an output of type T, and potentially throws an%0A   * Exception.%0A   * @param %3CT%3E - The return type.%0A   */%0A  public interface CheckedSupplier%3CT%3E %7B%0A    T get() throws Exception;%0A  %7D%0A%0A  /**%0A   * An interface for a function that takes no argument, returns no output, and potentially throws an Exception.%0A   */%0A  public interface CheckedRunnable %7B%0A    void run() throws Exception;%0A  %7D%0A%0A  /**%0A   * Runs a given function and catches any exceptions. If there are no exceptions, the function returns, as usual.%0A   * If there are any exceptions, the following steps occur. First, a stacktrace is printed. Then, if the exception%0A   * is of type TException, it is re-thrown. Otherwise, its message and simple name are wrapped%0A   * in a ServerLogicException and the ServerLogicException is thrown.%0A   * @param fn - The function to execute.%0A   * @param %3CT%3E - The return type of the function.%0A   * @return The return value of the function.%0A   * @throws TException - The exception thrown by fn(). If fn() does not produce a TException, this will be a%0A   * ServerLogicException (a subclass of TException).%0A   */
 %0A  p
@@ -1836,16 +1836,138 @@
  %7D%0A  %7D%0A%0A
+  /**%0A   * This is like run(CheckedSupplier%3CT%3E fn), but it first ensures that the given experiment run ID is valid.%0A   */%0A
   public

Comment ExceptionWrapper