scala-async icon indicating copy to clipboard operation
scala-async copied to clipboard

Document how error handling works

Open acjay opened this issue 10 years ago • 6 comments

How are failed futures handled? Does the whole async block just fail? Is there a way to handle individual failed awaits within one async block, or is the only solution there to nest async blocks? If that's the case, perhaps a tryAwait that returns a Try would be helpful. In any case, a couple examples in the docs would be great. Thanks

acjay avatar Jun 20 '14 13:06 acjay

I would also be interested in this.

justjoheinz avatar Nov 12 '14 10:11 justjoheinz

The code behaves as if the call to await throws the exception with which the awaited Future failed.

While await under try/catch is currently unsupported, one alternative would be to write await(future recover ...).

tryAwait would be equivalent to this simple function: def tryAwait[T](fut: Future[T]): Try[T] = await(fut map Success.apply recover PartialFunction(Failure.apply)

However, that won't work because await can't be used inside a function; a macro is needed. I'll submit a PR with that macro if you'd be interested in including tryAwait in the project.

(From what I've read of the code, most of the work will be to construct the right kind of Tryy. Why is there even a Tryy type if both implementations use scala.util.Try?)

danarmak avatar Oct 18 '15 10:10 danarmak

+1 to tryAwait at least until proper try/catch handling is supported

pkolaczk avatar Oct 20 '15 06:10 pkolaczk

I'll submit a PR with that macro if you'd be interested in including tryAwait in the project.

This would be much appreciated!

acjay avatar Jan 07 '16 23:01 acjay

I was playing with async/await and this is one of the first questions that popped up. So :+1: to improve this.

ebruchez avatar Nov 02 '16 14:11 ebruchez

FWIW I used @danarmak's solution above:

await(kittens() map Success.apply recover PartialFunction(Failure.apply)) match {
  case Failure(t) ⇒ // ...
  case Success(v) ⇒ // ...
}

But a tryAwait shortcut would be good.

ebruchez avatar Nov 03 '16 16:11 ebruchez