문제

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.

도움이 되었습니까?

해결책

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
}

다른 팁

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")
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top