If you want to recover a future with another future, you have to use recoverWith
:
wsParams.get().recoverWith {
case ex: ConnectException => wsParams.get()
case ex: SSLException => // do something else
}
Pregunta
There is not a lot of documentation about how to handle exceptions for WS. Specifically I would like to get a URL with a timeout and to try again that same url up to 5 times. I tried (simplistically) to nest in .recover but no luck.
val wsParams = WS.url(destinationUrl).withTimeout(5000)
val weResponse = wsParams.get().recover(
wsParams.get()
)
This throws: type mismatch; found : scala.concurrent.Future[play.api.libs.ws.Response] required: PartialFunction[Throwable,?]
Appreciate the help.
Solución
If you want to recover a future with another future, you have to use recoverWith
:
wsParams.get().recoverWith {
case ex: ConnectException => wsParams.get()
case ex: SSLException => // do something else
}
Otros consejos
I have used this library with success: https://github.com/softprops/retry
EX:
// define your success condition
implicit val success = new retry.Success[WSResponse](r => !((500 to 599) contains r.status))
// retry 3 times pausing 30 seconds in between attempts
val future = retry.Pause(3, 30.seconds) { () =>
WS.url("theUrl").post("postdata")
}