If you start out with a Try
from the get go with your for-comp then you can eliminate the match at the end. You can do this by forcing the Option
to a Try
via fold
. Here's what that could look like:
def getValidBRefForReferencedA(aRef: ARef): Try[BRef] = {
for {
a <- get[A](aRef).fold[Try[A]](Failure[A](new OtherException("Invalid aRef supplied")))(Success(_))
abRef = a.bRef
_ <- validBRefs.find(_ == abRef).fold[Try[BRef]](Failure(new MismatchException("No B found matching A's B-ref")))(Success(_))
} yield abRef
}
With this approach, you can get different exceptions for the two different checks. It's not perfect, but maybe it will work for you.