We use Twitter futures (as part of the Finagle stack) and I don't like the concept of using (business) exceptions to control the flow of our application, because exceptions don't show up in method signatures.
So I had the idea to use Future[Either[A,B]] as a replacement.
But I have some problems in using for comprehensions over futures with this concept:
E.g. we have a repository method:
def getUserCredentialsByNickname(nickname: String): Future[Either[EntityNotFound, UserCredentials]]
and a handler method which uses this repo and does some other checks and also creates a token
def process(request: LoginRequest): Future[Either[Failure, Login]] = {
for {
credentialsEither <- userRepository.getUserCredentialsByNickname(request.username)
...several other calls/checks which should 'interrupt' this for comprehension
token <- determineToken(credentials)
} yield token
The calls in the for comprehension after the getUserCredentialsByNickname(..) should only be executed if this call returns a Right[UserCredentials], but also the detailed error information from each returned Either should be returned from the handler.
Try
data structure which is already integrated with Finagle futures: twitter.github.io/util/util-core/target/site/doc/main/api/com/…Future[Either[EntityNotFound, UserCredentials]]
I see immediately what Failure types can be returned. When I use a try, I have to look at the implementation of the code handling the Try.