Java 8 CompletionStage exceptionally rethrow exception

473 views
Skip to first unread message

Pavan

unread,
Nov 18, 2016, 12:37:37 PM11/18/16
to Play Framework

Rethrowing an exception inside of exceptionally method of CompletionStage seems not to be allowed.

I need to check for a certain kind of exception and if not I need to rethrow it back :

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 errors out saying unhandledException java.lang.Throwable

What interface would possibly allow to rethrow back the exception ? Or is there a better way around ?

Reply all
Reply to author
Forward
0 new messages