traffichazard
traffichazard

Reputation: 1295

How do I get exception details from an Akka Supervisor?

I'm testing how a new Actor I'm working on handles unexpected messages. I'd like to assert that it throws a GibberishException in these cases. Here's the test and the implementation so far:

Test:

"""throw a GibberishException for unrecognized messages""" in {
  //define a service that creates gibberish-speaking repositories
  val stubs = new svcStub(
    actorOf(new Actor{
      def receive = { case _ => {
          self.channel ! "you're savage with the cabbage"
        }
      }
    })
  )
  val model = actorOf(new HomeModel(stubs.svc,stubs.store))
  val supervisor = Supervisor(
    SupervisorConfig(
      OneForOneStrategy(List(classOf[Exception]), 3, 1000),
      Supervise(model,Permanent) :: Nil
    )
  )
  try{
    intercept[GibberishException] {
      supervisor.start
      model !! "plan"
    }
  } finally {
    supervisor.shutdown
  }
  stubs.store.plan should equal (null)
  stubs.svcIsOpen should be (false)
}

Implementation:

class HomeModel(service: PlanService, store: HomeStore)
extends Actor {
  private val loaderRepo = service.getRepo()
  private var view: Channel[Any] = null

  override def postStop() = {
    service.close()
  }

  def receive = {
    case "plan" => {
      view=self.channel
      loaderRepo ! LoadRequest()
    }
    case p: Plan => {
      store.plan=p
      view ! store.plan
    }
    case _ => throw new GibberishException(_)
  }
}

However, when I run the test, the exception details get to the Supervisor I established, but I don't know how to do anything with them (like log them or test their type). I'd like to be able to get the exception details here from the supervisor so i can rethrow and intercept them in my test. Outside of a test method, I could imagine this being useful if you wanted to report the nature of an exception in the UI of a running app. Is there a way to get this from the Supervisor when it happens?

Upvotes: 1

Views: 964

Answers (1)

Viktor Klang
Viktor Klang

Reputation: 26597

Change the OneForOneStrategy to only handle GibberishException, should solve it.

Upvotes: 3

Related Questions