Skip to content

Instantly share code, notes, and snippets.

@renanreismartins
Created November 30, 2019 22:32
Show Gist options
  • Save renanreismartins/2c544b147dc334d1b2ee0c9c3fd92775 to your computer and use it in GitHub Desktop.
Save renanreismartins/2c544b147dc334d1b2ee0c9c3fd92775 to your computer and use it in GitHub Desktop.
Trying to understand Try vavr api
@Test
public void test() {
loadName(1)
.onSuccess(n -> log(format("onSuccess before recovery: %s", n)))
.andThen(n -> log(format("and then log this before recovery: %s", n)))
.onFailure(e -> log("error before recovery"))
.recoverWith(IllegalArgumentException.class, e -> loadNameFromFallback())
.onSuccess(n -> log(format("onSuccess after recovery: %s", n)))
.onSuccess(n -> log(format("another onSuccess: %s", n)))
.andThen(n -> log(format("and then log this after recovery: %s", n)))
.onFailure(e -> log("error after recovery"));
}
private Try<String> loadName(int id) {
if (id == 1) {
return Try.success("renan");
} else {
return Try.failure(new IllegalArgumentException());
}
}
private Try<String> loadNameFromFallback() {
return Try.success("renan from fallback");
}
private void log(String s) {
System.out.println(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment