I think part of your issue is that in Main.scala, you are first performing a blocking Await.result
before you register your non-blocking onComplete
callback. With Await.result
, if the Future
is failed, whatever exception the Failure
is wrapping will be thrown all the way out. So, in your case, if the Future
is failed, you will never get to the code where you register the onComplete
callback. If you remove the Await.result
, things should work as expected for you, more gracefully failing and hitting your Failure
case in the onComplete
callback.
In addition, in asFuture
, I'm not sure why you need that Promise
. You already created a Future
and can just return that as opposed to using a Promise
. Your code could be simplified to:
def asFuture(): Future[Job] = {
val fail = false
future {
while (!job.isDone()) {
Thread.sleep(1000)
}
if (fail)
throw new LameExcuse("Service unavailable")
job
}
}