Question

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.

Was it helpful?

Solution

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
}

OTHER TIPS

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")
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top