我在嘗試處理負責數據插入的休息服務上的服務器錯誤時遇到問題。使用runAsync時播放框架處理錯誤
public CompletableFuture<Result> insertSomething() throws IOException {
JsonNode jsNode = request().body().asJson();
format json node to be used
}
return CompletableFuture.runAsync(() -> {
try {
service.insertSomething(something);
} catch (ParseException e) {
internalServerError();
}
})
.thenApply(future -> created("data inserted"))
.exceptionally(ex -> internalServerError());
}
internalServerError永遠不會被拋出,它一直說「數據插入」。即使我發送一些拋出ParseException的數據。而在調試模式下,我看到它傳入了catch,但沒有拋出。
提前致謝
其實,你只是消音的'ParseException'。你打算在'catch'塊中寫一個'throw'語句嗎?如果是這樣,你需要拋出一個'RuntimeException',這裏'CompletionException'是最合適的,例如'拋出新的CompletionException(e);'。 – acelent