/** * Closes all {@link AutoCloseable} objects in the parameter, suppressing exceptions. Exception will be emitted * after calling close() on every object. * * @param closeables iterable with closeables to close. * @throws Exception collected exceptions that occurred during closing */ public static void closeAll(Iterable<? extends AutoCloseable> closeables) throws Exception { if (null != closeables) { Exception collectedExceptions = null; for (AutoCloseable closeable : closeables) { try { if (null != closeable) { closeable.close(); } } catch (Exception e) { collectedExceptions = ExceptionUtils.firstOrSuppressed(collectedExceptions, e); } } if (null != collectedExceptions) { throw collectedExceptions; } } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { logger.debug("Unhandled exception: {}", cause); sendError(ctx, ExceptionUtils.stringifyException(cause)); }
@Override public void close() throws Exception { lock.lock(); try { // close only once if (closed) { return; } closed = true; Throwable exception = null; try { blobStoreService.close(); } catch (Throwable t) { exception = t; } // we do not propagate exceptions here, but only log them try { hadoopFileSystem.close(); } catch (Throwable t) { exception = ExceptionUtils.firstOrSuppressed(t, exception); } if (exception != null) { ExceptionUtils.rethrowException(exception, "Could not properly close the YarnHighAvailabilityServices."); } } finally { lock.unlock(); } }
public static void suppressExceptions(RunnableWithException action) { try { action.run(); } catch (InterruptedException e) { // restore interrupted state Thread.currentThread().interrupt(); } catch (Throwable t) { if (isJvmFatalError(t)) { rethrow(t); } } }
/** * Copies all the files from the given stream state handles to the given path, renaming the files w.r.t. their * {@link StateHandleID}. */ private void downloadDataForAllStateHandles( Map<StateHandleID, StreamStateHandle> stateHandleMap, Path restoreInstancePath, CloseableRegistry closeableRegistry) throws Exception { try { List<Runnable> runnables = createDownloadRunnables(stateHandleMap, restoreInstancePath, closeableRegistry); List<CompletableFuture<Void>> futures = new ArrayList<>(runnables.size()); for (Runnable runnable : runnables) { futures.add(CompletableFuture.runAsync(runnable, executorService)); } FutureUtils.waitForAll(futures).get(); } catch (ExecutionException e) { Throwable throwable = ExceptionUtils.stripExecutionException(e); throwable = ExceptionUtils.stripException(throwable, RuntimeException.class); if (throwable instanceof IOException) { throw (IOException) throwable; } else { throw new FlinkRuntimeException("Failed to download data for state handles.", e); } } }
private static Predicate<Throwable> isConnectionProblemException() { return (throwable) -> ExceptionUtils.findThrowable(throwable, java.net.ConnectException.class).isPresent() || ExceptionUtils.findThrowable(throwable, java.net.SocketTimeoutException.class).isPresent() || ExceptionUtils.findThrowable(throwable, ConnectTimeoutException.class).isPresent() || ExceptionUtils.findThrowable(throwable, IOException.class).isPresent(); }
/** * Closes this streamer. * * @throws IOException */ public void close() throws IOException { Throwable throwable = null; try { socket.close(); sender.close(); receiver.close(); } catch (Throwable t) { throwable = t; } try { destroyProcess(process); } catch (Throwable t) { throwable = ExceptionUtils.firstOrSuppressed(t, throwable); } ShutdownHookUtil.removeShutdownHook(shutdownThread, getClass().getSimpleName(), LOG); ExceptionUtils.tryRethrowIOException(throwable); }
ExceptionUtils.rethrowException(error, error.getMessage());
/** * Unpacks an {@link ExecutionException} and returns its cause. Otherwise the given * Throwable is returned. * * @param throwable to unpack if it is an ExecutionException * @return Cause of ExecutionException or given Throwable */ public static Throwable stripExecutionException(Throwable throwable) { return stripException(throwable, ExecutionException.class); }
private Tuple2<ResponseBody, HttpResponseStatus> errorResponse(Throwable throwable) { Throwable error = ExceptionUtils.stripCompletionException(throwable); if (error instanceof RestHandlerException) { final RestHandlerException rhe = (RestHandlerException) error; log.error("Exception occurred in REST handler.", rhe); return Tuple2.of(new ErrorResponseBody(rhe.getMessage()), rhe.getHttpResponseStatus()); } else { log.error("Implementation error: Unhandled exception.", error); String stackTrace = String.format("<Exception on server side:%n%s%nEnd of exception on server side>", ExceptionUtils.stringifyException(throwable)); return Tuple2.of( new ErrorResponseBody(Arrays.asList("Internal server error.", stackTrace)), HttpResponseStatus.INTERNAL_SERVER_ERROR); } }
@Override public Map<String, OptionalFailure<Object>> getAccumulators(final JobID jobID, ClassLoader loader) throws Exception { final JobAccumulatorsHeaders accumulatorsHeaders = JobAccumulatorsHeaders.getInstance(); final JobAccumulatorsMessageParameters accMsgParams = accumulatorsHeaders.getUnresolvedMessageParameters(); accMsgParams.jobPathParameter.resolve(jobID); accMsgParams.includeSerializedAccumulatorsParameter.resolve(Collections.singletonList(true)); CompletableFuture<JobAccumulatorsInfo> responseFuture = sendRequest( accumulatorsHeaders, accMsgParams); Map<String, OptionalFailure<Object>> result = Collections.emptyMap(); try { result = responseFuture.thenApply((JobAccumulatorsInfo accumulatorsInfo) -> { try { return AccumulatorHelper.deserializeAccumulators( accumulatorsInfo.getSerializedUserAccumulators(), loader); } catch (Exception e) { throw new CompletionException( new FlinkException( String.format("Deserialization of accumulators for job %s failed.", jobID), e)); } }).get(timeout.toMillis(), TimeUnit.MILLISECONDS); } catch (ExecutionException ee) { ExceptionUtils.rethrowException(ExceptionUtils.stripExecutionException(ee)); } return result; }
/** * Converts a {@link ThrowingRunnable} into a {@link Runnable} which throws all checked exceptions * as unchecked. * * @param throwingRunnable to convert into a {@link Runnable} * @return {@link Runnable} which throws all checked exceptions as unchecked. */ static Runnable unchecked(ThrowingRunnable<?> throwingRunnable) { return () -> { try { throwingRunnable.run(); } catch (Throwable t) { ExceptionUtils.rethrow(t); } }; } }
default void close() throws Exception { try { closeAsync().get(); } catch (ExecutionException e) { throw new FlinkException("Could not close resource.", ExceptionUtils.stripExecutionException(e)); } }
/** * Rethrows the given {@code Throwable}, if it represents an error that is fatal to the JVM. * See {@link ExceptionUtils#isJvmFatalError(Throwable)} for a definition of fatal errors. * * @param t The Throwable to check and rethrow. */ public static void rethrowIfFatalError(Throwable t) { if (isJvmFatalError(t)) { throw (Error) t; } }
(KvStateLocation kvStateLocation, Throwable throwable) -> { if (throwable != null) { if (ExceptionUtils.stripCompletionException(throwable) instanceof FlinkJobNotFoundException) {
@Override public void onFailure(ActionRequest action, Throwable failure, int restStatusCode, RequestIndexer indexer) throws Throwable { if (ExceptionUtils.findThrowable(failure, EsRejectedExecutionException.class).isPresent()) { indexer.add(action); } else { // rethrow all other failures throw failure; } }
ExceptionUtils.rethrowException(error, error.getMessage());
/** * Unpacks an {@link CompletionException} and returns its cause. Otherwise the given * Throwable is returned. * * @param throwable to unpack if it is an CompletionException * @return Cause of CompletionException or given Throwable */ public static Throwable stripCompletionException(Throwable throwable) { return stripException(throwable, CompletionException.class); }
Throwable throwable = ExceptionUtils.stripExecutionException(e); throwable = ExceptionUtils.stripException(throwable, RuntimeException.class); if (throwable instanceof IOException) { throw (IOException) throwable;
/** * Convert a {@link BiConsumerWithException} into a {@link BiConsumer}. * * @param biConsumerWithException BiConsumer with exception to convert into a {@link BiConsumer}. * @param <A> first input type * @param <B> second input type * @return {@link BiConsumer} which rethrows all checked exceptions as unchecked. */ static <A, B> BiConsumer<A, B> unchecked(BiConsumerWithException<A, B, ?> biConsumerWithException) { return (A a, B b) -> { try { biConsumerWithException.accept(a, b); } catch (Throwable t) { ExceptionUtils.rethrow(t); } }; } }