Skip to content

Instantly share code, notes, and snippets.

@dkomanov
Created May 23, 2020 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkomanov/db7aab87894cdfdde44d72a6281958cd to your computer and use it in GitHub Desktop.
Save dkomanov/db7aab87894cdfdde44d72a6281958cd to your computer and use it in GitHub Desktop.
[writing-async-app-in-scala-part-2] safeFuture implementation
def safeFuture[T](f: => Future[T]): Future[T] = {
try {
f
} catch {
case NonFatal(e) => Future.failed(e)
}
}
def handleFromMemcached(r: HttpRequest): Future[Option[Int]] = safeFuture {
require(r.uri != "/memcached-boom", "Memcached Boom!")
Future.successful(if (r.uri == "/memcached") Some(42) else None)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment