Patterns for Handling Completion Stage and Exceptions together?

627 views
Skip to first unread message

Kamal raj

unread,
Jun 21, 2017, 10:54:30 AM6/21/17
to Play Framework
Hi Play Team, 

I'm learning Play in my side project. I have a code like this:

Long userId = 0L;

try {
    String user = tokenGenerator.getUserNameForToken(token);
    //other code over here
} catch (SignatureException e) {
    //should be an authentication error message
}

if(userId != 0)
    return dashBoardService.createDashBoard().handle(((userdataRecord, throwable) -> {
        ObjectNode response = Json.newObject();
        response.put("success","true");

        return ok(response);
    }));
else
{
    //pass on the authentication error message
    //in catch block as a response
}

The above code should return the type `CompletionStage<Result>`. Since the call to getUserNameForToken may throw exception, I need to handle that as well as success case. I'm not sure how one should send an dummy CompletionStage just to mock for an error (which is actually captured in the catch block). 

May be I'm doing it wrong someway or there should be a common pattern for the same. 

Will Sargent

unread,
Jun 21, 2017, 12:44:09 PM6/21/17
to play-fr...@googlegroups.com
You probably want completionStage.exceptionally(), or call handle / handleAsync.


--
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/8e389f59-15b0-48db-9b8b-d5d123ace9ce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages