Sebastien Lorber Sebastien Lorber - 4 years ago 84
Scala Question

Scala Try/Future, wrapping the exception in case of failure

Suppose I have a method

def doSomething: String
which can raise a
if something does wrong.

If I write
, is there a simple way to map the exception without recovering it?

Basically, I want the failure to become a
caused by the

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 can't find anything in the API.

Lee Lee
Answer Source

You can use transform

val t = Failure(new DoSomethingException)
val bt = t.transform(s => Success(s), e => Failure(new BusinessException))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download