0

I'm trying to write an utility which automatically propagate checked exception in a reactiv way without writing boiler plate code with static block inside my operators:

    public class ReactRethrow {
        public static <T, R> Function<T, R> rethrow(Function<T, R> catchedFunc) {

            return t -> {
            try {
                return catchedFunc.apply(t);
            } catch (Exception e) {
                throw Exceptions.propagate(e);
            }
        };
    }
}

but it stil complaining about IOException here:

Flux.fromArray(resources).map(ReactRethrow.rethrow(resource -> Paths.get(resource.getURI())))

any idea?

bodtx
  • 590
  • 9
  • 29
  • `Exceptions.propagate(e)` wraps a checked exception into a special runtime exception that can be handled by `onError` https://stackoverflow.com/a/61580265/4785824 – quintin May 03 '20 at 19:17

1 Answers1

1

Well for a reason I do not clearly understand You have to take as parameter a function which throw exceptions and so declare a specific functionalInterface:

public class ReactRethrow {
    public static <T, R> Function<T, R> rethrow(FunctionWithCheckeException<T, R> catchedFunc) {

        return t -> {
            try {
                return catchedFunc.call(t);
            } catch (Exception e) {
                throw Exceptions.propagate(e);
            }
        };
    }

    @FunctionalInterface
    public interface FunctionWithCheckeException<T, R> {
        R call(T t) throws Exception;
    }

}

from here https://leoniedermeier.github.io/docs/java/java8/streams_with_checked_exceptions.html

bodtx
  • 590
  • 9
  • 29