Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[scala] result code: either
def processRequestEither(userId: UUID, requestId: UUID): BusinessResult = {
val result: Either[BusinessResult, BusinessResult] = for {
user <- either(getUser(userId), BusinessResult.UserNotFound)
request <- either(getRequestById(requestId), BusinessResult.RequestNotFound)
_ <- either(checkAccess(request, user).toOption, BusinessResult.NotOwner)
} yield BusinessResult.Ok
result.fold(identity, identity)
}
private def either[T](opt: Option[T], result: BusinessResult) =
opt.fold[Either[BusinessResult, T]](Left(result))(Right(_)).right
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment