bodtx
bodtx

Reputation: 609

rxjava reactor, Utility to propagate exception

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?

Upvotes: 0

Views: 452

Answers (1)

bodtx
bodtx

Reputation: 609

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

Upvotes: 1

Related Questions