我想在使用VAVR(以前称为javaslang)时记录异常。以下是示例代码段。
//will get 500 as response from this url
String sampleurl = "http://someurl.com";
List<String> myList = List.of(sampleurl);
LOGGER.info("In {} with urls {}",getClass(),myList);
return Observable.from(myList).flatMap(url ->
Observable.create(subscriber -> {
Try<String> httpEntity = HttpUtil.retrieveData(url).flatMap(httpResponse -> Try.of( () -> EntityUtils.toString(httpResponse.getEntity())));
httpEntity
.andThen(subscriber::onNext)
.andThen(subscriber::onCompleted)
.onFailure(subscriber::onError);
}));
我正在尝试在onFailure()块中记录异常,但没有记录任何内容。请告诉我这个。
此致 洁
答案 0 :(得分:1)
在这两种情况下,成功和失败,Vavr都按预期工作。这是简单的测试:
// prints nothing
Try.success("ok")
.andThen(() -> {})
.andThen(() -> {})
.onFailure(System.out::println);
// prints "java.lang.Error: ok"
Try.failure(new Error("ok"))
.andThen(() -> {})
.andThen(() -> {})
.onFailure(System.out::println);
我看到两个可能的答案,为什么未在您的示例中记录失败:
Try
Disclamer:我是Vavr的创建者