How can I handle if the future returns a failed exception?
The scenario is that my code calls getValue() , matches the result with verifyValue() , and then I want to be able to handle the case where the result of getValue () is Future.failed(new Exception("message")) . However, when I run this, if the result of getValue () is a bad future, it just throws an exception rather than handling it.
Does anyone have any suggestions on how I will do this?
def method(): Future[JsObject] = { getValue().flatMap(verifyValue(_)) } def getValue(): Future[JsObject] = { try { value1 <- getValue1() value2 <- getValue2(value1) } yield { value2 } } def verifyValue(result: Any): Future[JsObject] = { result match { case e: Exception => getValue() case json: JsObject => Future.successful(json) } }
Update: I don’t think I made it clear with the original question, but the reason I am planning the value is because I don’t want to explicitly wait for any of the futures in my code, and therefore I don’t want to use Future.onComplete {} functions for determining the value.
Update 2: Another thing that may be unclear is that if it throws an exception, I want to call another method. I don’t want it to simply handle the exception, it will register an exception and then call another method whose return value is of the same type as getValue ().
scala
annedroiid
source share