Scala: How to determine the exception type of a Failure

error-handling, exception, scala

Solution

case Success(f) => 
case Failure(e: ExceptionType1) =>
case Failure(e: ExceptionType2) => 
case Failure(e) => // other

or

case Success(f) =>
case Failure(e) => e match {
   case e1: ExceptionType1 =>
   case e2: ExceptioNType2 =>
   case _ => 
}

Problem

Look at this code snippet: ``` userService.insert(user) match { case Success(f) => Logger.debug("User created successfully") case Failure(e) => { // how do I determine the type of `e`? } } ``` How do I determine the type of the exception contained by `Failure`? I need to take different actions depending on the exception type.

Original source