[jax-rs-spec users] Re: Returning CompletionStage from a resource method

From: Markus KARG <>
Date: Tue, 25 Apr 2017 22:19:13 +0200

Throwing CancellationException sounds correct to me, and I think it would be a good idea to mandate that the fact of cancellation is mentioned in the error message when returning status 500.


-----Original Message-----
From: Pavel Bucek []
Sent: Dienstag, 25. April 2017 09:56
Subject: Returning CompletionStage from a resource method

Dear experts,

I was implementing support for returning CompletionStage from a resource method and I found some room for possible inconsistency:

When returned CompletionStage is already done, we don't need to spawn another thread, we just use the same one. To be able to call "isDone", the implementation must convert CompletionStage to CompletableFuture (there is a method on the CompletionStage interface for that, so no issues so far).

CompletableFuture has three terminal states:

- completed,
- completed exceptionally,
- cancelled.

When the state is completed, it is the same as when the resource method would return the type directly.

When the state is completed exceptionally, it is the as when the resource method would thrown an exception.

The question is what to do when the state is cancelled?

My initial implementation throws IllegalStateException, but when looking to what is passed to CompletionStage.whenComplete, maybe the better way would be to throw CancellationException. (assuming that both, ISE or CE would be then processable by ExceptionMapper, if/when registered).

Any ideas/opinions on which exception should be thrown?

Thanks and regards,

ps.: after seeing what I wrote, I convinced myself to use CancellationException - anyway, if anyone has other opinion, please share it.