Created
January 9, 2016 18:10
-
-
Save dkomanov/bf3592d34448c90a11c2 to your computer and use it in GitHub Desktop.
[scala] result code: use exceptions internally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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