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 BooleanFutureExtensions(val future: Future[Boolean]) extends AnyVal { | |
def orFail(e: => Throwable): Future[Boolean] = | |
future.transform(t => if (t.isSuccess && !t.get) Failure(e) else t) | |
} |
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 | |
if !movie.hidden | |
actors <- getActors(movie) | |
plot <- getPlot(movie) | |
} yield MovieDescription(movie, actors, plot) |
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
movie <- getMovie.filter(!_.hidden).recoverWith { | |
case _: NoSuchElementException => Future.failed(new HiddenMovieException("...")) | |
} |
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 filterOrFail(f: T => Boolean, e: => Throwable): Future[T] = | |
future.flatMap(value => if (f(value)) Future.successful(value) else Future.failed(e)) | |
} |
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) |
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 { | |
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 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
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
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 |