@Override public void cleanup(TransactionManager resource, Throwable initThrowable) { // Propagate errors, but there's no need to do cleanup as each task is responsible for that, // and this class assumes the tasks are independent. if (!(initThrowable instanceof NotInitializedException)) { throw Throwables.rewrapAndThrowUncheckedException(initThrowable); } } }
@Override public byte[] persistToBytes(JsonNode jsonNode) { try { return mapper.writeValueAsBytes(jsonNode); } catch (JsonProcessingException e) { throw Throwables.throwUncheckedException(e); } }
@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)); } }
public static RuntimeException throwUncheckedException(Throwable ex) { throwIfInstance(ex, RuntimeException.class); throwIfInstance(ex, Error.class); throw createPalantirRuntimeException(ex); }
private static <T> T executeOnTimeLock(Callable<T> callable) { try { return callable.call(); } catch (Exception e) { if (e.getCause() instanceof ConnectException || e.getCause() instanceof UnknownHostException || e.getCause() instanceof NotCurrentLeaderException) { throw Throwables.unwrapAndThrowAtlasDbDependencyException(e); } else { throw Throwables.throwUncheckedException(e); } } }
@Test public void notAllowSimpleGetsOnThoroughTables() throws IllegalAccessException { Method[] declaredMethods = ReadTransaction.class.getDeclaredMethods(); for (Method method : declaredMethods) { // Ignore methods that are either not simple gets or are overloaded if (simpleGets.containsKey(method.getName()) && hasExpectedParameterCount(method)) { checkThrowsAndNoInteraction(() -> { try { method.invoke(readTransaction, simpleGets.get(method.getName())); } catch (InvocationTargetException e) { Throwables.throwIfInstance(e.getCause(), IllegalStateException.class); } catch (IllegalAccessException e) { Throwables.throwUncheckedException(e); } }, IllegalStateException.class, "Cannot read"); } } }
/** * 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); }
/** * Will simply rethrow the exception if it is a {@link RuntimeException} or an {@link Error} */ public static void throwIfUncheckedException(Throwable ex) { throwIfInstance(ex, RuntimeException.class); throwIfInstance(ex, Error.class); }
@SuppressWarnings("unchecked") T rv = (T) c.newInstance(newMessage); return chain(rv, throwable); addCurrentStackIfRewrapFails(throwable); return throwable; throw createPalantirRuntimeException(newMessage, throwable);
@SuppressWarnings("GuardedByChecker") private V getReturnValue() throws ExecutionException, CancellationException { if (cancellationException != null) { throw Throwables.chain(new CancellationException("This task was canceled before it ever ran."), cancellationException); } if (executionException != null) { throw new ExecutionException(executionException); } return returnValue; }
latch2.await(); } catch (InterruptedException e) { throw Throwables.throwUncheckedException(e); } catch (Throwable t1) { latch.countDown(); Throwables.throwIfInstance(t1, Exception.class); throw Throwables.throwUncheckedException(t1);
/** * 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 static RuntimeException createAtlasDbDependencyException(Throwable ex) { if (ex instanceof InterruptedException || ex instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } throwIfInstance(ex, AtlasDbDependencyException.class); return new AtlasDbDependencyException(ex); }
public static RuntimeException throwUncheckedException(Throwable ex) { throwIfInstance(ex, RuntimeException.class); throwIfInstance(ex, Error.class); throw createPalantirRuntimeException(ex); }
@SuppressWarnings("unchecked") T rv = (T) c.newInstance(newMessage); return chain(rv, throwable); addCurrentStackIfRewrapFails(throwable); return throwable; throw createPalantirRuntimeException(newMessage, throwable);
protected String convertReaderToString(final InputStreamReader reader) throws PalantirSqlException { if (reader == null){ return null; } try{ final int SIZE = 1024; char[] cbuf = new char[SIZE]; int length; StringBuilder sb = new StringBuilder(); while ( (length = reader.read(cbuf, 0, SIZE)) > 0){ sb.append(cbuf, 0, length); } return sb.toString(); } catch (IOException e){ throw Throwables.chain(PalantirSqlException.createForChaining(), Throwables.chain(new SQLException("Could not convert BufferedReader into a String"), e)); //$NON-NLS-1$ } }
/** * Cleanup to be done if init() throws, before init() can be attempted again. If this method throws, runWithRetry() * will fail and init() will not be retried. The default implementation assumes that init() throwing is terminal * and there is no cleanup necessary. This should be overridden by specifying any cleanup steps necessary, and the * method must return instead of throwing if init() can be retried * * @param initException Throwable thrown by init() */ public void cleanup(R resource, Throwable initException) { throw Throwables.rewrapAndThrowUncheckedException(initException); }
@Override public void close() { try { closable.close(); } catch (IOException e) { Throwables.throwUncheckedException(e); } } };
@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)); } }
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); } }