Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: How to determine the exception type of a Failure

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.

like image 384
j3d Avatar asked Jan 05 '14 23:01

j3d


1 Answers

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 _ => 
}
like image 65
Didier Dupont Avatar answered Oct 03 '22 23:10

Didier Dupont