How to catch a java.util.concurrent.TimeoutException in 2.5

311 views
Skip to first unread message

Sandy Miller

unread,
Jan 20, 2017, 5:48:40 AM1/20/17
to Play Framework
I am using Play 2.5 and cannot catch a Timeout exception. Here is my code.


    WSRequest request = ws.url(serviceURL)
.setHeader("Authorization", Application.authorizationHeader())
.setRequestTimeout(1800000);


return request.post( postBody ).thenApplyAsync( response -> {
JsonNode json = response.asJson();

if ( response.getStatus() == OK || response.getStatus() == CREATED ) {

return created( json );

} else if ( response.getStatus() == REQUEST_TIMEOUT ) {

return internalServerError();

} else {

return internalServerError( json );
}
} );

I put try and catch around this code but still cannot catch it.

try {
WSRequest request = ws.url(serviceURL)


} catch (Exception e){

}

Will Sargent

unread,
Jan 20, 2017, 11:47:03 AM1/20/17
to play-fr...@googlegroups.com
Can you try using thenApply rather than thenApplyAsync?  That's probably the most direct route.

You may want to use an async timeout i.e.


Play 2.6 will have a built in timeout class that will make this easier.

--
Will Sargent
Engineer, Lightbend, Inc.


--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/76af6510-24a6-422d-b9ee-bf5a0580e4ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Greg Methvin

unread,
Jan 20, 2017, 2:08:16 PM1/20/17
to play-framework
Hi Sandy,

Play's WS client is asynchronous. Instead of throwing the exception directly, the error is returned in a failed CompletionStage.

You could use exceptionally to handle the exception, or use handle/handleAsync instead of thenApplyAsync.

Greg

On Thu, Jan 19, 2017 at 7:12 AM, Sandy Miller <sapm...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/76af6510-24a6-422d-b9ee-bf5a0580e4ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Tech Lead - Play Framework

Reply all
Reply to author
Forward
0 new messages