Skip to content

Instantly share code, notes, and snippets.

@msulima
Last active December 16, 2015 20:49
Show Gist options
  • Save msulima/5495242 to your computer and use it in GitHub Desktop.
Save msulima/5495242 to your computer and use it in GitHub Desktop.
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import org.scalatest.FunSuite
class ErrorsTest extends FunSuite {
test("Future can recover from error") {
val numberFuture: Future[Int] = generateNumber()
val addedFuture: Future[Int] = numberFuture.map(addThree).recover({
case exception: IllegalStateException => -1
})
val added: Int = Await.result(addedFuture, 1 seconds)
assert(added == -1)
}
private def generateNumber(): Future[Int] = Future {
Thread.sleep(100)
throw new IllegalStateException()
}
def addThree(value: Int): Int =
value + 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment