Wrap exceptions in vavr Try

(, en)

In current vavr (0.10.3), I really miss a generic mapFailure that I can use to wrap an earlier caught exception in a new exception. I came up with this helper funcion:

public class TryUtil {
  public static <T, R> API.Match.Case<T, R> wrap(Function<? super T, ? extends R> f) {
    Objects.requireNonNull(f, "f is null");

    return API.Case(API.$(), f);
  }
}

It can be used like this:

return Try.of(() -> s3Service.getObjectAsString(key))
        .mapFailure(wrap(ex -> new ProcessingException(key, ex)));