user2668128
user2668128

Reputation: 42302

Handling Unhandled Akka Errors in Play Framework with Scala

I'm using Akka in my play application and mostly it's working out really. However, one challenge I have is unhandled exceptions.

I use the ask pattern like this in the controller:

(searcher ? request) map {

Where searcher is an actor created by the actor system.

Searcher then sends a message to another actor. And this is the problem. How can I get the unhandled exception back to the controller so I know there was an unhandled exception and can tell the user something went wrong.

As far as I can tell, every actor needs a reference to the anonymous actor created by the ask pattern in the code above.

Normally errors would propagate up to the supervisor. But in this case, the supervisor would have no reference the anonymous actor created by the ask. So it seems each actor will need a reference to the anonymous actor and catch and send their own errors (losing the benefits of supervision hierarchy).

Upvotes: 0

Views: 842

Answers (2)

Sergiy Prydatchenko
Sergiy Prydatchenko

Reputation: 988

You can send errors wrapped in akka.actor.Status.Failure from your searcher to the sender automatically using pipeto:

Future { ... } pipeTo sender

or in case of chained processing:

(anotherSearcher ? anotherRequest) pipeTo sender

or you can handle and send errors manually:

try {
  ...
} catch {
  case e: Exception ⇒
    sender ! akka.actor.Status.Failure(e)
}

And then you can handle them in your controller:

val theFuture = searcher ? request

theFuture onSuccess {
  case ... =>
    self ! SuccessMessage(...)
    // or process in a different way (don't forget that this will be executed in a separate thread - e.g. don't close over the sender)
}

theFuture onFailure {
  case e =>
   self ! FailureMessage(e)
   // or process in a different way (don't forget that this will be executed in a separate thread - e.g. don't close over the sender)
}

Upvotes: 1

1esha
1esha

Reputation: 1722

Normally it does not matters for your users where there error is. All you need is to show 500 error page. You can use ask timeout to do this.

Also, you can log error from certain actor to your log and investigate it by operations.

So, you simple do not need to know what there error is when you ask your searcher. All you need to know is timeout exception.

Upvotes: 0

Related Questions