/** * Rewraps the given throwable in a newly created throwable of the same runtime type in order to capture the current * thread's stack trace. Use this method when you are about to rethrow a throwable from another thread, * for example when throwing {@link ExecutionException#getCause()} after calling {@link Future#get()}; */ public static <T extends Throwable> T rewrap(T throwable) { Preconditions.checkNotNull(throwable); return rewrap(throwable.getMessage(), throwable); }
@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)); } }
@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. */ @SuppressWarnings("unchecked") public static <K extends Throwable> void rewrapAndThrowIfInstance(String newMessage, Throwable t, Class<K> clazz) throws K { if ((t != null) && clazz.isAssignableFrom(t.getClass())) { K kt = (K) t; K wrapped = Throwables.rewrap(newMessage, kt); throw wrapped; } }
private void throwIfInvalidSetup(PingableLeader cachedService, PingableLeader pingedService, String pingedServiceUuid) { if (cachedService == null) { return; } IllegalStateException exception = new IllegalStateException( "There is a fatal problem with the leadership election configuration! " + "This is probably caused by invalid pref files setting up the cluster " + "(e.g. for lock server look at lock.prefs, leader.prefs, and lock_client.prefs)." + "If the preferences are specified with a host port pair list and localhost index " + "then make sure that the localhost index is correct (e.g. actually the localhost)."); if (cachedService != pingedService) { log.error("Remote potential leaders are claiming to be each other!", exception); throw Throwables.rewrap(exception); } if (pingedServiceUuid.equals(getUUID())) { log.error("Remote potential leader is claiming to be you!", exception); throw Throwables.rewrap(exception); } }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (!isJavaLangObjectMethod(method) && throwException.get()) { throw Throwables.rewrap(exception); } try { if (method.getName().equals("equals")) { // We are trying to check proxy equality, not that of the delegate return proxy == args[0]; } return method.invoke(delegate, args); } catch (InvocationTargetException e) { throw e.getCause(); } }
UnsafeArg.of("full path", file.getAbsolutePath()), SafeArg.of("file name", file.getName())); throw Throwables.rewrap(e); } finally { IOUtils.closeQuietly(fileIn);
SafeArg.of("runId", runId), SafeArg.of("failureCount", failureCount), e); throw Throwables.rewrap(String.format("Failing after %d tries.", failureCount), e);
/** * Rewraps the given throwable in a newly created throwable of the same runtime type in order to capture the current * thread's stack trace. Use this method when you are about to rethrow a throwable from another thread, * for example when throwing {@link ExecutionException#getCause()} after calling {@link Future#get()}; */ public static <T extends Throwable> T rewrap(T throwable) { Preconditions.checkNotNull(throwable); return rewrap(throwable.getMessage(), throwable); }
@Test public void testRewrap() { try { throwTwoArgConstructorException(); fail("Should not get here"); } catch (TwoArgConstructorException e) { TwoArgConstructorException wrapped = Throwables.rewrap(e); assertEquals(e.getMessage(), wrapped.getMessage()); assertSame(e, wrapped.getCause()); } try { throwSQLException(); fail("Should not get here"); } catch (SQLException e) { SQLException wrapped = Throwables.rewrap(e); assertEquals(e.getMessage(), wrapped.getMessage()); assertSame(e, wrapped.getCause()); } try { throwNoUsefulConstructorException(); fail("Should not get here"); } catch (NoUsefulConstructorException e) { int sizeBefore = e.getStackTrace().length; NoUsefulConstructorException wrapped = Throwables.rewrap(e); assertSame(e, wrapped); int sizeAfter = e.getStackTrace().length; assertTrue(sizeAfter + " should be > " + sizeBefore, sizeAfter > sizeBefore); } }
/** * 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. */ @SuppressWarnings("unchecked") public static <K extends Throwable> void rewrapAndThrowIfInstance(String newMessage, Throwable t, Class<K> clazz) throws K { if ((t != null) && clazz.isAssignableFrom(t.getClass())) { K kt = (K) t; K wrapped = Throwables.rewrap(newMessage, kt); throw wrapped; } }
private void throwIfInvalidSetup(PingableLeader cachedService, PingableLeader pingedService, String pingedServiceUuid) { if (cachedService == null) { return; } IllegalStateException exception = new IllegalStateException( "There is a fatal problem with the leadership election configuration! " + "This is probably caused by invalid pref files setting up the cluster " + "(e.g. for lock server look at lock.prefs, leader.prefs, and lock_client.prefs)." + "If the preferences are specified with a host port pair list and localhost index " + "then make sure that the localhost index is correct (e.g. actually the localhost)."); if (cachedService != pingedService) { log.error("Remote potential leaders are claiming to be each other!", exception); throw Throwables.rewrap(exception); } if (pingedServiceUuid.equals(getUUID())) { log.error("Remote potential leader is claiming to be you!", exception); throw Throwables.rewrap(exception); } }
UnsafeArg.of("full path", file.getAbsolutePath()), SafeArg.of("file name", file.getName())); throw Throwables.rewrap(e); } finally { IOUtils.closeQuietly(fileIn);
SafeArg.of("runId", runId), SafeArg.of("failureCount", failureCount), e); throw Throwables.rewrap(String.format("Failing after %d tries.", failureCount), e);