diff --git a/src/main/java/io/vavr/control/Try.java b/src/main/java/io/vavr/control/Try.java index 88fb8f246..a5121a5a9 100644 --- a/src/main/java/io/vavr/control/Try.java +++ b/src/main/java/io/vavr/control/Try.java @@ -1132,6 +1132,22 @@ public final Either toEither() { } } + /** + * Converts this {@code Try} to an {@link Either}, converting the Throwable (in the failure case) + * to a left value using the passed {@link Function}. + * + * @param left type of the resulting Either + * @param throwableMapper A transformation from throwable to the left type of the new {@code Either} + * @return A new {@code Either} + */ + public final Either toEither(Function throwableMapper) { + if (isFailure()) { + return Either.left(throwableMapper.apply(getCause())); + } else { + return Either.right(get()); + } + } + /** * Converts this {@code Try} to a {@link Validation}. * @@ -1142,8 +1158,8 @@ public final Validation toValidation() { } /** - * Converts this {@code Try} to a {@link Validation}, converting the Throwable (if present) - * to another object using passed {@link Function}. + * Converts this {@code Try} to a {@link Validation}, converting the Throwable (in the failure case) + * to another object using the passed {@link Function}. * *
{@code
      * Validation = Try.of(() -> 1/0).toValidation(Throwable::getMessage));
diff --git a/src/test/java/io/vavr/control/TryTest.java b/src/test/java/io/vavr/control/TryTest.java
index ce7a36fd3..b4bb9da36 100644
--- a/src/test/java/io/vavr/control/TryTest.java
+++ b/src/test/java/io/vavr/control/TryTest.java
@@ -969,6 +969,13 @@ public void shouldConvertFailureToEitherLeftSupplier() {
         assertThat(failure().toEither(() -> "test").isLeft()).isTrue();
     }
 
+    @Test
+    public void shouldConvertFailureToEitherLeftMapper() {
+        final Try failure = Try.failure(new RuntimeException("a certain value"));
+        final Either either = failure.toEither(Throwable::getMessage);
+        assertThat(either).isEqualTo(Either.left("a certain value"));
+    }
+
 
     // -- toValidation