public void join() { try { statusFuture.get(); } catch (Exception e) { throw Throwables.propagate(e); } }
@Override public Response toResponse(Throwable throwable) { if (throwable instanceof WebApplicationException) { return ((WebApplicationException) throwable).getResponse(); } log.warn(throwable, "Request failed for %s", request.getRequestURI()); ResponseBuilder responseBuilder = Response.serverError() .header(CONTENT_TYPE, TEXT_PLAIN); if (includeExceptionInResponse) { responseBuilder.entity(Throwables.getStackTraceAsString(throwable)); } else { responseBuilder.entity("Exception processing request"); } return responseBuilder.build(); } }
/** * Stores the given throwable and rethrows it. It will be rethrown as is if it is an {@code * IOException}, {@code RuntimeException}, {@code Error} or a checked exception of the given type. * Otherwise, it will be rethrown wrapped in a {@code RuntimeException}. <b>Note:</b> Be sure to * declare all of the checked exception types your try block can throw when calling an overload of * this method so as to avoid losing the original exception type. * * <p>This method always throws, and as such should be called as {@code throw closer.rethrow(e, * ...);} to ensure the compiler knows that it will throw. * * @return this method does not return; it always throws * @throws IOException when the given throwable is an IOException * @throws X when the given throwable is of the declared type X */ public <X extends Exception> RuntimeException rethrow(Throwable e, Class<X> declaredType) throws IOException, X { checkNotNull(e); thrown = e; Throwables.propagateIfPossible(e, IOException.class); Throwables.propagateIfPossible(e, declaredType); throw new RuntimeException(e); }
private static <T> T doAs(Subject subject, GssSupplier<T> action) throws GSSException { try { return Subject.doAs(subject, (PrivilegedExceptionAction<T>) action::get); } catch (PrivilegedActionException e) { Throwable t = e.getCause(); throwIfInstanceOf(t, GSSException.class); throwIfUnchecked(t); throw new RuntimeException(t); } }
/** * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link * RuntimeException}, {@link Error}, {@code declaredType1}, or {@code declaredType2}. In the * unlikely case that you have three or more declared checked exception types, you can handle them * all by invoking these methods repeatedly. See usage example in {@link * #propagateIfPossible(Throwable, Class)}. * * @param throwable the Throwable to possibly propagate * @param declaredType1 any checked exception type declared by the calling method * @param declaredType2 any other checked exception type declared by the calling method */ @GwtIncompatible // propagateIfInstanceOf public static <X1 extends Throwable, X2 extends Throwable> void propagateIfPossible( @Nullable Throwable throwable, Class<X1> declaredType1, Class<X2> declaredType2) throws X1, X2 { checkNotNull(declaredType2); propagateIfInstanceOf(throwable, declaredType1); propagateIfPossible(throwable, declaredType2); }
@Override public void oneDeclared() throws SomeCheckedException { try { methodThatThrowsOtherChecked(); } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, SomeCheckedException.class); throw Throwables.propagate(t); } } };
@Override public void run() { try { wrapped.call(); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } } };
/** * @return the cause message of {@link #getError()}, Optional.empty() is error is not set. */ public Optional<String> getErrorMessage() { return error != null ? Optional.ofNullable(getRootCause(error).getMessage()) : Optional.empty(); }
@Override public void oneDeclared() throws SomeCheckedException { try { methodThatDoesntThrowAnything(); } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, SomeCheckedException.class); throw Throwables.propagate(t); } } };
private Object invokeGeneratorMethod(Method generator, Object... args) { try { return generator.invoke(this, args); } catch (InvocationTargetException e) { throwIfUnchecked(e.getCause()); throw new RuntimeException(e.getCause()); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } }
/** * Propagates {@code throwable} exactly as-is, if and only if it is an instance of {@link * RuntimeException}, {@link Error}, or {@code declaredType}. Example usage: * * <pre> * try { * someMethodThatCouldThrowAnything(); * } catch (IKnowWhatToDoWithThisException e) { * handle(e); * } catch (Throwable t) { * Throwables.propagateIfPossible(t, OtherException.class); * throw new RuntimeException("unexpected", t); * } * </pre> * * @param throwable the Throwable to possibly propagate * @param declaredType the single checked exception type declared by the calling method */ @GwtIncompatible // propagateIfInstanceOf public static <X extends Throwable> void propagateIfPossible( @Nullable Throwable throwable, Class<X> declaredType) throws X { propagateIfInstanceOf(throwable, declaredType); propagateIfPossible(throwable); }
public Grok cachedGrokForPattern(String pattern, boolean namedCapturesOnly) { try { if (namedCapturesOnly) { return grokCacheNamedOnly.get(pattern); } else { return grokCache.get(pattern); } } catch (UncheckedExecutionException | ExecutionException e) { final Throwable rootCause = Throwables.getRootCause(e); log.error("Unable to load grok pattern {} into cache", pattern, rootCause); throw new RuntimeException(rootCause); } }
private static <K, V> Map<K, V> getAll(LoadingCache<K, V> cache, Iterable<K> keys) { try { return cache.getAll(keys); } catch (ExecutionException | UncheckedExecutionException e) { throwIfInstanceOf(e.getCause(), PrestoException.class); throwIfUnchecked(e); throw new UncheckedExecutionException(e); } }
public static byte[] toUtf8(final String string) { try { return string.getBytes(UTF8_STRING); } catch (UnsupportedEncodingException e) { // Should never happen throw Throwables.propagate(e); } }
@Override public void oneDeclared() throws SomeCheckedException { try { methodThatThrowsChecked(); } catch (Throwable t) { Throwables.propagateIfInstanceOf(t, SomeCheckedException.class); throw Throwables.propagate(t); } } };
/** * Stores the given throwable and rethrows it. It will be rethrown as is if it is an {@code * IOException}, {@code RuntimeException}, {@code Error} or a checked exception of either of the * given types. Otherwise, it will be rethrown wrapped in a {@code RuntimeException}. <b>Note:</b> * Be sure to declare all of the checked exception types your try block can throw when calling an * overload of this method so as to avoid losing the original exception type. * * <p>This method always throws, and as such should be called as {@code throw closer.rethrow(e, * ...);} to ensure the compiler knows that it will throw. * * @return this method does not return; it always throws * @throws IOException when the given throwable is an IOException * @throws X1 when the given throwable is of the declared type X1 * @throws X2 when the given throwable is of the declared type X2 */ public <X1 extends Exception, X2 extends Exception> RuntimeException rethrow( Throwable e, Class<X1> declaredType1, Class<X2> declaredType2) throws IOException, X1, X2 { checkNotNull(e); thrown = e; Throwables.propagateIfPossible(e, IOException.class); Throwables.propagateIfPossible(e, declaredType1, declaredType2); throw new RuntimeException(e); }
/** Returns the most concrete public methods from {@code type}. */ private static Method[] getMostConcreteMethods(Class<?> type) { Method[] methods = type.getMethods(); for (int i = 0; i < methods.length; i++) { try { methods[i] = type.getMethod(methods[i].getName(), methods[i].getParameterTypes()); } catch (Exception e) { throwIfUnchecked(e); throw new RuntimeException(e); } } return methods; }
public <T> DescriptionBuilder expected(String reason) { description.appendText(reason).appendText("\nExpected to not be reachable"); description.appendText("\nLocation: ").appendText( Throwables.getStackTraceAsString(new Exception())); matches = false; return this; }
/** * 获取异常的Root Cause. * * 如无底层Cause, 则返回自身 * * @see Throwables#getRootCause(Throwable) */ public static Throwable getRootCause(@NotNull Throwable t) { return Throwables.getRootCause(t); }
@Override public String serializeToString(T obj) { try { return jsonMapper.writeValueAsString(obj); } catch (JsonProcessingException e) { throw Throwables.propagate(e); } }