Skip to content

Instantly share code, notes, and snippets.

@outime
Created January 30, 2017 08:14
Show Gist options
  • Save outime/2be39706c541e2f92a785e4104124e87 to your computer and use it in GitHub Desktop.
Save outime/2be39706c541e2f92a785e4104124e87 to your computer and use it in GitHub Desktop.
How to handle timeout with WS api in Play2.1/Scala 2.10
def asFutureEither[T](f: Future[T]): Future[Either[Throwable, T]] =
f map { Right(_) } recover { case x => Left(x) }
import play.api._
import play.api.mvc._
import play.api.libs.ws.WS
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits._
import java.util.concurrent.TimeoutException
object Application extends Controller {
def index = Action {
Async {
val request = WS.url("https://api.github.com/repos/playframework/Play20/commits")
.withTimeout(3000).get
request map { response =>
Ok(response.json)
} recover {
case t: TimeoutException =>
RequestTimeout(t.getMessage)
case e =>
ServiceUnavailable(e.getMessage)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment