exceptionally
CompletionStageのメソッド内で例外を再実行することはできないようです。Java 8 CompletionStage例外的に例外を戻す
:
Future<JsonNode> futureSite = someClient.getSite(siteId, queryParams);
CompletionStage<JsonNode> outcome = FutureConverters.toJava(futureSite);
return outcome.thenApplyAsync((siteJson) -> {
Site site = Json.fromJson(siteJson, Site.class);
try {
return function.apply(site);
} catch (RequestException e) {
return e.result;
}
}, httpExecutionContext.current()).exceptionally(throwable -> {
if(throwable instanceof SomeClientException) {
if(((SomeClientException) throwable).httpStatusCode == 404) {
return entityNotFound("Site", siteId);
}
}
// let JSON parsing failures and other errors bubble up, TODO play2.5
throw throwable;
});
throw throwable
エラーうち、おそらく戻って再スローすることができるようになるどのようなインターフェースunhandledException java.lang.Throwable
を言って例外?それとも良い方法がありますか?
更新:
BaseController.java :
protected class RequestException extends Exception {
private static final long serialVersionUID = -2154871100650903869L;
public Result result;
public RequestException(Result result) {
this.result = result;
}
}
@FunctionalInterface
protected interface RequestFunction<T, R> {
R apply(T t) throws RequestException;
}
protected CompletionStage<Result> performWithSite(final Long siteId, RequestFunction<Site, Result> function) {
QueryParams queryParams = QueryParams.create();
Future<JsonNode> futureSite = someClient.getSite(siteId, queryParams);
CompletionStage<JsonNode> outcome = FutureConverters.toJava(futureSite);
return handleSpecific(
outcome.thenApplyAsync(siteJson -> {
Site site = Json.fromJson(siteJson, Site.class);
try {
return function.apply(site);
} catch (RequestException e) {
return e.result;
}
}, httpExecutionContext.current()),
throwable -> throwable instanceof SomeClientException
&& ((SomeClientException)throwable).httpStatusCode == 404,
() -> entityNotFound("Site", siteId));
}
protected Result entityNotFound(String entityName, String id) {
// building our custom error model.
Error e = new Error(
Http.Status.NOT_FOUND,
ErrorCode.ENTITY_NOT_FOUND,
ErrorCodes.NOT_FOUND, new String[]{entityName, id});
return notFound(e.asJson());
}
ので、上記のコードの本質は、私は次のとおりです。私は以下のようにホルガーからアドバイスをしようとしたが、私は実際にこれを再スロー可能性がどの時点ではまだ確認していない
someClientにアクセスして、サイトが存在し、クライアントがSomeClientExceptionをスローできるかどうかを確認する必要があります。Controller.java
public Result destroy(Long siteId, Long productId){
return performWithSite(siteId, site -> {
productWriter.deleteProduct(siteId, productId);
return noContent();
}).toCompletableFuture().exceptionally(e -> {
Logger.error(e+"exception");
throw e.getCause();
// above line errors out as unhandledException java.lang.throwable, I need the NotFoundException which is contained within the CompletionException to be thrown.
});
}
//上記のコントローラコードでは、クライアントにリモート呼び出しを行ってサイトが存在するかどうかを確認した後、製品を削除する必要があります。
productWriter.deleteProduct(siteId, productId)
はまだ投げるかもしれないNotFoundException
か、私は戻ってそれを再スローする必要がある何か他のもの... ので、コントローラのコードから再スローされる例外は、呼び出しチェーンにおける当社のカスタム例外ハンドラをピックアップしていること。
"return 404 when deleting a nonexistent Product" {
when(productDAO.findBySiteAndProductId(anyLong(), anyLong())) thenReturn null
a[NotFoundException] should be thrownBy { controller.destroy(0L, 1L) }
}
質問はプレイ固有ではないと思います –