Example usage for com.google.common.base Throwables propagateIfInstanceOf

List of usage examples for com.google.common.base Throwables propagateIfInstanceOf

Introduction

In this page you can find the example usage for com.google.common.base Throwables propagateIfInstanceOf.

Prototype

public static <X extends Throwable> void propagateIfInstanceOf(@Nullable Throwable throwable,
        Class<X> declaredType) throws X 

Source Link

Document

Propagates throwable exactly as-is, if and only if it is an instance of declaredType .

Usage

From source file:com.bigfatgun.fixjures.FixtureException.java

public static <T> T convertAndThrowAs(final Throwable cause) throws FixtureException {

    Throwables.propagateIfInstanceOf(cause, FixtureException.class);

    final FixtureException converted = new FixtureException(cause);
    converted.peelTopOfStackTrace();//ww  w  .j  a  va  2  s. com
    throw converted;
}

From source file:org.trustedanalytics.cfbroker.store.zookeeper.service.CuratorExceptionHandler.java

public static <R> R propagateAsIOException(FunctionThatThrows<R> function, Consumer<String> logging,
        String message) throws IOException {

    try {/*w ww  .  j  av  a2 s  . c  om*/
        return function.apply();
    } catch (Exception e) {
        logging.accept(message);
        Throwables.propagateIfInstanceOf(e, IOException.class);
        Throwables.propagateIfInstanceOf(e, RuntimeException.class);
        throw new IOException(e);
    }
}

From source file:com.metamx.common.RetryUtils.java

public static <T> T retry(final Callable<T> f, Predicate<Throwable> shouldRetry, final int maxTries)
        throws Exception {
    Preconditions.checkArgument(maxTries > 0, "maxTries > 0");
    int nTry = 0;
    while (true) {
        try {/*from   w  ww .j  a  v  a  2 s  . c o m*/
            nTry++;
            return f.call();
        } catch (Throwable e) {
            if (nTry < maxTries && shouldRetry.apply(e)) {
                awaitNextRetry(e, nTry);
            } else {
                Throwables.propagateIfInstanceOf(e, Exception.class);
                throw Throwables.propagate(e);
            }
        }
    }
}

From source file:org.apache.druid.java.util.common.guava.Yielders.java

public static <T> Yielder<T> done(final T finalVal, final AutoCloseable closeable) {
    return new Yielder<T>() {
        @Override// ww w . j  a v a 2s. c om
        public T get() {
            return finalVal;
        }

        @Override
        public Yielder<T> next(T initValue) {
            return null;
        }

        @Override
        public boolean isDone() {
            return true;
        }

        @Override
        public void close() throws IOException {
            if (closeable != null) {
                try {
                    closeable.close();
                } catch (Exception e) {
                    Throwables.propagateIfInstanceOf(e, IOException.class);
                    throw Throwables.propagate(e);
                }
            }
        }
    };
}

From source file:de.cosmocode.palava.scope.ThrowingDestroyErrors.java

@Override
public void destroyError(Object object, Exception cause) {
    Throwables.propagateIfInstanceOf(cause, DestroyException.class);
    throw new DestroyException(cause);
}

From source file:com.google.gitiles.doc.GitilesMarkdown.java

public static RootNode parseFile(GitilesView view, String path, String md) {
    if (md == null) {
        return null;
    }//from  w  w  w .  j  a  va2  s.c  o m

    try {
        try {
            return newParser().parseMarkdown(md.toCharArray());
        } catch (ParserRuntimeException e) {
            Throwables.propagateIfInstanceOf(e.getCause(), ParsingTimeoutException.class);
            throw e;
        }
    } catch (ParsingTimeoutException e) {
        log.error("timeout rendering {}/{} at {}", view.getRepositoryName(), path,
                view.getRevision().getName());
        return null;
    }
}

From source file:io.druid.java.util.common.RetryUtils.java

/**
 * Retry an operation using fuzzy exponentially increasing backoff. The wait time after the nth failed attempt is
 * min(60000ms, 1000ms * pow(2, n - 1)), fuzzed by a number drawn from a Gaussian distribution with mean 0 and
 * standard deviation 0.2./*w  ww  .  j  a v a  2s  .c om*/
 *
 * If maxTries is exhausted, or if shouldRetry returns false, the last exception thrown by "f" will be thrown
 * by this function.
 *
 * @param f           the operation
 * @param shouldRetry predicate determining whether we should retry after a particular exception thrown by "f"
 * @param quietTries  first quietTries attempts will log exceptions at DEBUG level rather than WARN
 * @param maxTries    maximum number of attempts
 *
 * @return result of the first successful operation
 *
 * @throws Exception if maxTries is exhausted, or shouldRetry returns false
 */
public static <T> T retry(final Callable<T> f, Predicate<Throwable> shouldRetry, final int quietTries,
        final int maxTries) throws Exception {
    Preconditions.checkArgument(maxTries > 0, "maxTries > 0");
    int nTry = 0;
    while (true) {
        try {
            nTry++;
            return f.call();
        } catch (Throwable e) {
            if (nTry < maxTries && shouldRetry.apply(e)) {
                awaitNextRetry(e, nTry, nTry <= quietTries);
            } else {
                Throwables.propagateIfInstanceOf(e, Exception.class);
                throw Throwables.propagate(e);
            }
        }
    }
}

From source file:org.sonatype.nexus.quartz.JobSupport.java

@Override
public void execute(final JobExecutionContext context) throws JobExecutionException {
    MDC.put(JobSupport.class.getSimpleName(), getClass().getSimpleName());
    this.context = context;
    try {/*  w w  w .  j ava 2 s.c  o m*/
        execute();
    } catch (Exception e) {
        Throwables.propagateIfInstanceOf(e, JobExecutionException.class);
        throw new JobExecutionException(e);
    } finally {
        this.context = null;
        MDC.remove(JobSupport.class.getSimpleName());
    }
}

From source file:org.sonatype.guice.bean.reflect.AbstractDeferredClass.java

@SuppressWarnings("finally")
public final T get() {
    try {//  www . j  a v  a 2  s. c  om
        // load class and bootstrap injection
        return injector.getInstance(load());
    } catch (final Throwable e) {
        for (Throwable t = e; t != null; t = t.getCause()) {
            Throwables.propagateIfInstanceOf(t, ThreadDeath.class);
        }
        try {
            Logs.warn("Error injecting: {}", getName(), e);
        } finally {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            }
            throw new ProvisionException("Error injecting: " + getName(), e);
        }
    }
}

From source file:com.facebook.presto.operator.scalar.MapGenericEquality.java

public static Boolean genericEqual(MethodHandle keyEqualsFunction, MethodHandle keyHashcodeFunction,
        Type keyType, Block leftMapBlock, Block rightMapBlock, EqualityPredicate predicate) {
    Map<KeyWrapper, Integer> wrappedLeftMap = new LinkedHashMap<>();
    for (int position = 0; position < leftMapBlock.getPositionCount(); position += 2) {
        wrappedLeftMap.put(new KeyWrapper(readNativeValue(keyType, leftMapBlock, position), keyEqualsFunction,
                keyHashcodeFunction), position + 1);
    }//from  w w w .  j a va2 s  . c om

    Map<KeyWrapper, Integer> wrappedRightMap = new LinkedHashMap<>();
    for (int position = 0; position < rightMapBlock.getPositionCount(); position += 2) {
        wrappedRightMap.put(new KeyWrapper(readNativeValue(keyType, rightMapBlock, position), keyEqualsFunction,
                keyHashcodeFunction), position + 1);
    }

    if (wrappedLeftMap.size() != wrappedRightMap.size()) {
        return false;
    }

    for (Map.Entry<KeyWrapper, Integer> entry : wrappedRightMap.entrySet()) {
        KeyWrapper key = entry.getKey();
        Integer leftValuePosition = wrappedLeftMap.get(key);
        if (leftValuePosition == null) {
            return false;
        }

        try {
            Boolean result = predicate.equals(leftValuePosition, entry.getValue());
            if (result == null) {
                return null;
            } else if (!result) {
                return false;
            }
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, Error.class);
            Throwables.propagateIfInstanceOf(t, PrestoException.class);

            throw new PrestoException(GENERIC_INTERNAL_ERROR, t);
        }
    }
    return true;
}