/** * Transforms a failure of the given type (potentially into a value) by applying the given function to it. * <p> * This method is similar to {@link #mapError(Class, Function)}, except that it allows async transformation. * * @param function the transformation to apply to the promise failure * @return a promise * @since 1.3 */ default <E extends Throwable> Promise<T> flatMapError(Class<E> type, Function<? super E, ? extends Promise<T>> function) { return transform(up -> down -> up.connect(down.onError(throwable -> { if (type.isInstance(throwable)) { Promise<T> transformed; try { transformed = function.apply(type.cast(throwable)); } catch (Throwable t) { down.error(t); return; } transformed.connect(down); } else { down.error(throwable); } })) ); }
/** * Transforms a failure of the given type (potentially into a value) by applying the given function to it. * <p> * This method is similar to {@link #mapError(Function)}, except that it will only apply if the error is of the given type. * If the error is not of the given type, it will not be transformed and will propagate as normal. * * @param function the transformation to apply to the promise failure * @return a promise * @since 1.3 */ default <E extends Throwable> Promise<T> mapError(Class<E> type, Function<? super E, ? extends T> function) { return transform(up -> down -> up.connect(down.onError(throwable -> { if (type.isInstance(throwable)) { T transformed; try { transformed = function.apply(type.cast(throwable)); } catch (Throwable t) { down.error(t); return; } down.success(transformed); } else { down.error(throwable); } })) ); }
public static <T> void retryAttempt(int attemptNum, int maxAttempts, Upstream<? extends T> up, Downstream<? super T> down, BiFunction<? super Integer, ? super Throwable, Promise<Duration>> onError) throws Exception { up.connect(down.onError(e -> { if (attemptNum > maxAttempts) { down.error(e);
/** * Transforms a failure of the given type (potentially into a value) by applying the given function to it. * <p> * This method is similar to {@link #mapError(Function)}, except that it allows async transformation. * * @param function the transformation to apply to the promise failure * @return a promise * @since 1.3 */ default Promise<T> flatMapError(Function<? super Throwable, ? extends Promise<T>> function) { return transform(up -> down -> up.connect(down.onError(throwable -> { Promise<T> transformed; try { transformed = function.apply(throwable); } catch (Throwable t) { down.error(t); return; } transformed.connect(down); })) ); }
up.connect(down.onError(throwable -> { if (predicate.apply(throwable)) { try {
up.connect(down.onError(throwable -> { try { T transformed = transformer.apply(throwable);
/** * Transforms a failure of the given type (potentially into a value) by applying the given function to it. * <p> * This method is similar to {@link #mapError(Predicate, Function)}, except that it allows async transformation. * * @param predicate the predicate to test against the error * @param function the transformation to apply to the promise failure * @return a promise * @since 1.6.0 */ default Promise<T> flatMapError(Predicate<? super Throwable> predicate, Function<? super Throwable, ? extends Promise<T>> function) { return transform(up -> down -> up.connect(down.onError(throwable -> { if (predicate.apply(throwable)) { Promise<T> transformed; try { transformed = function.apply(throwable); } catch (Throwable t) { down.error(t); return; } transformed.connect(down); } else { down.error(throwable); } })) ); }
/** * Transforms a failure of the given type (potentially into a value) by applying the given function to it. * <p> * This method is similar to {@link #mapError(Function)}, except that it will only apply depending if it satisfies the predicate. * If the error is not of the given type, it will not be transformed and will propagate as normal. * * @param predicate the predicate to test against the error * @param function the transformation to apply to the promise failure * @return a promise * @since 1.6.0 */ default Promise<T> mapError(Predicate<? super Throwable> predicate, Function<? super Throwable, ? extends T> function) { return transform(up -> down -> up.connect(down.onError(throwable -> { if (predicate.apply(throwable)) { T transformed; try { transformed = function.apply(throwable); } catch (Throwable t) { down.error(t); return; } down.success(transformed); } else { down.error(throwable); } })) ); }