/** * if (t instanceof K) throw Throwables.rewrap((K)t); * <p> * Note: The runtime type of the thrown exception will be the same as t even if * clazz is a supertype of t. */ public static <K extends Throwable> void rewrapAndThrowIfInstance(Throwable t, Class<K> clazz) throws K { rewrapAndThrowIfInstance(t == null ? "null" : t.getMessage(), t, clazz); }
/** * If Throwable is a RuntimeException or Error, rewrap and throw it. If not, throw a * new PalantirRuntimeException(ex) */ public static RuntimeException rewrapAndThrowUncheckedException(String newMessage, Throwable ex) { rewrapAndThrowIfInstance(newMessage, ex, RuntimeException.class); rewrapAndThrowIfInstance(newMessage, ex, Error.class); throw createPalantirRuntimeException(newMessage, ex); }
@Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { Future<Object> future = executor.submit(() -> { try { return method.invoke(delegate, args); } catch (InvocationTargetException e) { Throwables.rewrapAndThrowIfInstance(e.getCause(), Exception.class); Throwables.rewrapAndThrowIfInstance(e.getCause(), Error.class); throw Throwables.rewrapAndThrowUncheckedException(e.getCause()); } }); try { return future.get(); } catch (ExecutionException e) { throw Throwables.rewrap(e.getCause()); } catch (InterruptedException e) { throw new PalantirInterruptedException(e); } finally { future.cancel(cancel.equals(CancelDelegate.CANCEL)); } }
/** * if (t instanceof K) throw Throwables.rewrap((K)t); * <p> * Note: The runtime type of the thrown exception will be the same as t even if * clazz is a supertype of t. */ public static <K extends Throwable> void rewrapAndThrowIfInstance(Throwable t, Class<K> clazz) throws K { rewrapAndThrowIfInstance(t == null ? "null" : t.getMessage(), t, clazz); }
/** * If Throwable is a RuntimeException or Error, rewrap and throw it. If not, throw a * new PalantirRuntimeException(ex) */ public static RuntimeException rewrapAndThrowUncheckedException(String newMessage, Throwable ex) { rewrapAndThrowIfInstance(newMessage, ex, RuntimeException.class); rewrapAndThrowIfInstance(newMessage, ex, Error.class); throw createPalantirRuntimeException(newMessage, ex); }
@Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { Future<Object> future = executor.submit(() -> { try { return method.invoke(delegate, args); } catch (InvocationTargetException e) { Throwables.rewrapAndThrowIfInstance(e.getCause(), Exception.class); Throwables.rewrapAndThrowIfInstance(e.getCause(), Error.class); throw Throwables.rewrapAndThrowUncheckedException(e.getCause()); } }); try { return future.get(); } catch (ExecutionException e) { throw Throwables.rewrap(e.getCause()); } catch (InterruptedException e) { throw new PalantirInterruptedException(e); } finally { future.cancel(cancel.equals(CancelDelegate.CANCEL)); } }