This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def executeLazily[Argument, Return](argument: Argument, | |
list: List[Argument => Future[Option[Return]]]) | |
(implicit ec: ExecutionContext): Future[Option[Return]] = { | |
val promise = Promise[Option[Return]]() | |
val iterator = list.iterator | |
def completeWith(t: Try[Option[Return]]): Unit = t match { | |
case Success(value) => | |
if (value.isDefined || !iterator.hasNext) | |
promise.success(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val handlers: List[HttpRequest => Future[Option[Int]]] = List( | |
handleFromMemcached, | |
handleFromCdn, | |
r => handleFromHadoop(r).map(Some.apply) | |
) | |
def handle(r: HttpRequest): Future[Int] = { | |
executeLazily(handlers.map(handler => () => handler(r))) | |
.map(_.getOrElse(throw new IllegalStateException("Hadoop should always return Some!"))) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class HttpRequest(uri: String) | |
def handleFromMemcached(r: HttpRequest): Future[Option[Int]] = { | |
require(r.uri != "/memcached-boom", "Memcached Boom!") | |
Future.successful(if (r.uri == "/memcached") Some(42) else None) | |
} | |
def handleFromCdn(r: HttpRequest): Future[Option[Int]] = { | |
require(r.uri != "/cnd-boom", "CDN Boom!") | |
Future.successful(if (r.uri == "/cdn") Some(42) else None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def innocentFunction(param: AnyRef): Future[Int] = { | |
require(param != null) | |
Future.successful(42) | |
} | |
innocentFunction(null) | |
.map(_ => throw new IllegalStateException) | |
// an IllegalArgumentException will be thrown before map call |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future(42) | |
.map[Int](_ => throw new IllegalArgumentException) | |
.flatMap[Int](_ => throw new IllegalStateException) | |
// Effectively: Future.failed(new IllegalArgumentException) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class FutureExtensions[T](val future: Future[T]) extends AnyVal { | |
def recoverFilter(f: => T): Future[T] = | |
future.recover { | |
case _: NoSuchElementException | ControlException => f | |
} | |
def recoverFilterWith(f: => Future[T]): Future[T] = | |
future.recoverWith { | |
case _: NoSuchElementException | ControlException => f | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for { | |
movie <- findMovie("Dark Waters") orFail new MovieNotFoundException("...") | |
} yield movie |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit class FutureOfOptionExtensions[T](val v: Future[Option[T]]) extends AnyVal { | |
def orFail(e: => Throwable): Future[T] = | |
v.flatMap(_.fold[Future[T]](Future.failed(e), Future.successful) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for { | |
_ <- hasPermissions orFail new PermissionDeniedException("...") | |
movie <- getMovie.filterOrFail(!_.hidden, new HiddenMovieException("...")) | |
actors <- getActors(movie) | |
plot <- getPlot(movie) | |
} yield MovieDescription(movie, actors, plot) |