Suppose I have a method def doSomething: String
which can raise a DoSomethingException
if something goes wrong.
If I write Try(doSomething)
, is there a simple way to map the exception without recovering it?
Basically, I want the failure to become a BusinessException
caused by the DoSomethingException
.
I know the code to do this is very simple, but isn't there any built-in operator to do so? It seems a very common operation but I can't find anything in the API.
You can use transform
val t = Failure(new DoSomethingException)
val bt = t.transform(s => Success(s), e => Failure(new BusinessException))