- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
actor exception handling
Sun, 2009-10-04, 06:51
Hi, I have an actor that I'd like to be able to call synchronously or asynchronously:
val a = actor { loop { react { case json: String => val res = doSomethingWithJson(json) reply(res)} } }
I can call it via:
a ! someJson
or
val result = a !? someJson
This works great. The problem comes when doSomethingWithJson might throw an exception. In particular I have a unit test where I'd like to be able to do the following:
def test = { { a !? someBadJson } must throwA[Throwable]}
But of course this doesn't work as the exception gets eaten. Furthermore my test freezes so it appears the actor is not even active anymore. I know there's a straightforward way to do what I want but I haven't been able to find it online.
Any suggestions?
Thanks,Lincoln
val a = actor { loop { react { case json: String => val res = doSomethingWithJson(json) reply(res)} } }
I can call it via:
a ! someJson
or
val result = a !? someJson
This works great. The problem comes when doSomethingWithJson might throw an exception. In particular I have a unit test where I'd like to be able to do the following:
def test = { { a !? someBadJson } must throwA[Throwable]}
But of course this doesn't work as the exception gets eaten. Furthermore my test freezes so it appears the actor is not even active anymore. I know there's a straightforward way to do what I want but I haven't been able to find it online.
Any suggestions?
Thanks,Lincoln
Sun, 2009-10-04, 13:17
#2
Re: actor exception handling
Josh thanks for the info.
Really I'm only interested in !? for my tests. I can't think of a reason I wouldn't be calling ! all the time in production. Can you show me how to do #2?
Thanks,Lincoln
On Sun, Oct 4, 2009 at 6:01 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
Really I'm only interested in !? for my tests. I can't think of a reason I wouldn't be calling ! all the time in production. Can you show me how to do #2?
Thanks,Lincoln
On Sun, Oct 4, 2009 at 6:01 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
You should either
1) Handle the exception inside your actor and return an option/either (i.e. None for the exceptional case)
2) Create a supervisor actor and link it to "a". The supervisor actors should catch the Exit message and handle it appropriately.
In general, I'd recommend against !? if you can't guarantee that messages will always be returned and no deadlocks will happen.
- Josh
On Sun, Oct 4, 2009 at 1:51 AM, Lincoln <linxbetter@gmail.com> wrote:
Hi, I have an actor that I'd like to be able to call synchronously or asynchronously:
val a = actor { loop { react { case json: String => val res = doSomethingWithJson(json) reply(res)} } }
I can call it via:
a ! someJson
or
val result = a !? someJson
This works great. The problem comes when doSomethingWithJson might throw an exception. In particular I have a unit test where I'd like to be able to do the following:
def test = { { a !? someBadJson } must throwA[Throwable]}
But of course this doesn't work as the exception gets eaten. Furthermore my test freezes so it appears the actor is not even active anymore. I know there's a straightforward way to do what I want but I haven't been able to find it online.
Any suggestions?
Thanks,Lincoln
Sun, 2009-10-04, 13:27
#3
Re: actor exception handling
Also, will an uncaught exception cause the actor to stop accepting messages?
On Sun, Oct 4, 2009 at 8:15 AM, Lincoln <linxbetter@gmail.com> wrote:
On Sun, Oct 4, 2009 at 8:15 AM, Lincoln <linxbetter@gmail.com> wrote:
Josh thanks for the info.
Really I'm only interested in !? for my tests. I can't think of a reason I wouldn't be calling ! all the time in production. Can you show me how to do #2?
Thanks,Lincoln
On Sun, Oct 4, 2009 at 6:01 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
You should either
1) Handle the exception inside your actor and return an option/either (i.e. None for the exceptional case)
2) Create a supervisor actor and link it to "a". The supervisor actors should catch the Exit message and handle it appropriately.
In general, I'd recommend against !? if you can't guarantee that messages will always be returned and no deadlocks will happen.
- Josh
On Sun, Oct 4, 2009 at 1:51 AM, Lincoln <linxbetter@gmail.com> wrote:
Hi, I have an actor that I'd like to be able to call synchronously or asynchronously:
val a = actor { loop { react { case json: String => val res = doSomethingWithJson(json) reply(res)} } }
I can call it via:
a ! someJson
or
val result = a !? someJson
This works great. The problem comes when doSomethingWithJson might throw an exception. In particular I have a unit test where I'd like to be able to do the following:
def test = { { a !? someBadJson } must throwA[Throwable]}
But of course this doesn't work as the exception gets eaten. Furthermore my test freezes so it appears the actor is not even active anymore. I know there's a straightforward way to do what I want but I haven't been able to find it online.
Any suggestions?
Thanks,Lincoln
Sun, 2009-10-04, 17:47
#4
Re: actor exception handling
I tested and it seems like the answer is no which is good. I've decided to wrap my actor bodies in try/catch and return an Either. In my tests I call them using !? and otherwise I call them using !.
Is there any way within the body of an actor to know how it was called? I ask because my actors also pass messages to other actors and ideally, if I'm calling from a test I would like everything to be synchronous so that I can test results reliably.
Thanks,Lincoln
On Sun, Oct 4, 2009 at 8:16 AM, Lincoln <linxbetter@gmail.com> wrote:
Is there any way within the body of an actor to know how it was called? I ask because my actors also pass messages to other actors and ideally, if I'm calling from a test I would like everything to be synchronous so that I can test results reliably.
Thanks,Lincoln
On Sun, Oct 4, 2009 at 8:16 AM, Lincoln <linxbetter@gmail.com> wrote:
Also, will an uncaught exception cause the actor to stop accepting messages?
On Sun, Oct 4, 2009 at 8:15 AM, Lincoln <linxbetter@gmail.com> wrote:
Josh thanks for the info.
Really I'm only interested in !? for my tests. I can't think of a reason I wouldn't be calling ! all the time in production. Can you show me how to do #2?
Thanks,Lincoln
On Sun, Oct 4, 2009 at 6:01 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
You should either
1) Handle the exception inside your actor and return an option/either (i.e. None for the exceptional case)
2) Create a supervisor actor and link it to "a". The supervisor actors should catch the Exit message and handle it appropriately.
In general, I'd recommend against !? if you can't guarantee that messages will always be returned and no deadlocks will happen.
- Josh
On Sun, Oct 4, 2009 at 1:51 AM, Lincoln <linxbetter@gmail.com> wrote:
Hi, I have an actor that I'd like to be able to call synchronously or asynchronously:
val a = actor { loop { react { case json: String => val res = doSomethingWithJson(json) reply(res)} } }
I can call it via:
a ! someJson
or
val result = a !? someJson
This works great. The problem comes when doSomethingWithJson might throw an exception. In particular I have a unit test where I'd like to be able to do the following:
def test = { { a !? someBadJson } must throwA[Throwable]}
But of course this doesn't work as the exception gets eaten. Furthermore my test freezes so it appears the actor is not even active anymore. I know there's a straightforward way to do what I want but I haven't been able to find it online.
Any suggestions?
Thanks,Lincoln
1) Handle the exception inside your actor and return an option/either (i.e. None for the exceptional case)
2) Create a supervisor actor and link it to "a". The supervisor actors should catch the Exit message and handle it appropriately.
In general, I'd recommend against !? if you can't guarantee that messages will always be returned and no deadlocks will happen.
- Josh
On Sun, Oct 4, 2009 at 1:51 AM, Lincoln <linxbetter@gmail.com> wrote: