So - how can you at least address this situation; if you're like me, and like explicit understanding based on your Type arrows; how can you wrap this up in a way that gives you the context you desire:
def tryingFutTry[T](f: => Future[Try[T]])(implicit executionContext: ExecutionContext): Future[Try[T]] = try { f.recoverWith({ case e: Throwable => Future(Failure(e))}) } catch { case e: Exception => Future(Failure(e)) } def tryingFut[T](f: => Future[T])(implicit executionContext: ExecutionContext): Future[Try[T]] = try { f.map[Try[T]](Success.apply).recoverWith({ case e: Throwable => Future(Failure(e))}) } catch { case e: Exception => Future(Failure(e)) } def trying[T](f: => T)(implicit executionContext: ExecutionContext): Try[T] = try { Success(f) } catch { case e: Exception => Failure(e) }Whilst this isn't very pretty, or perhaps even very well named; I'm not loving it yet; it does at least give you a way to "lift" non-try wrapped Future in a Try context to lift out the failure case from inside the Future that gets eaten, and allow you to expose swallowed exceptions when you have an explicit Try context that may not be catching all exceptions.
A lot of these ideas were take from a blog post that I found here:
https://alvinalexander.com/scala/how-exceptions-work-scala-futures-oncomplete-failure
No comments:
Post a Comment