Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[scala] result code: use exceptions internally
def processRequestWithException(userId: UUID, requestId: UUID): BusinessResult = {
case class BusinessException(result: BusinessResult) extends RuntimeException
try {
val user = getUser(userId).getOrElse(throw new BusinessException(BusinessResult.UserNotFound))
val request = getRequestById(requestId).getOrElse(throw new BusinessException(BusinessResult.RequestNotFound))
checkAccess(request, user).toOption.getOrElse(throw new BusinessException(BusinessResult.NotOwner))
BusinessResult.Ok
} catch {
case be: BusinessException => be.result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment