Skip to content

Instantly share code, notes, and snippets.

@dkomanov
Created January 9, 2016 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkomanov/bf3592d34448c90a11c2 to your computer and use it in GitHub Desktop.
Save dkomanov/bf3592d34448c90a11c2 to your computer and use it in GitHub Desktop.
[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