Search code examples
akka

Propagating exception to root actor


I have actor hierarchy like a->b->c.

a sends message to b, which then sends message to c.

For certain scenarios, c needs to throw an exception(without shutting down) and only a has appropriate information to handle the exception. So exception thrown by c needs to reach a for proper handling, with b not part of this propagation.

What is idiomatic way of achieving this in akka?


Solution

  • Leaking exceptions across actors is generally a bad idea (which is why in Typed, for instance, supervision doesn't say why an actor failed, just that it failed): it severely breaks encapsulation to know why an actor failed: another actor needing to know the particulars of how another actor failed is a decent sign that these want to be the same actor.

    However, in situations where this sort of thing is the best/least-bad way to solve a problem, the general answer is that actor C will have to catch the exception (so that it doesn't blow up, at least not immediately), and send the exception or at least some part of it (very often only the message matters: the stack trace, for instance, is not likely to be meaningful) to actor A. This requires either sending the message with the exception to actor B (placing an expectation or requirement on B that it be able to forward it to A), or alternatively the message from B to C can include the ActorRef for A to introduce A to C for the purpose of receiving the exception. If A -> B -> C is a static hierarchy, this introduction can happen at actor construction, though if it's a static hierarchy, this sort of thing might be better done as a stream graph.