LAgent: an agent framework in F# – Part III – Default error management
Luca -
☕ 2 min. read
Download framework here.
All posts are here:
- Part I - Workers and ParallelWorkers
- Part II - Agents and control messages
- Part III - Default error management
- Part IV - Custom error management
- Part V - Timeout management
- Part VI - Hot swapping of code
- Part VII - An auction framework
- Part VIII — Implementing MapReduce (user model)
- Part IX — Counting words …
Default error management
What happens when an error occurs? Well, ideally you want to notify someone and continue processing messages. By default you want to print the error and as much information as you can about it.
Let’s first see what happens if you pass the wrong message type:
counter1 <-- "fst"
Generates:
**> The exception below occurred on agent Undefined at state 3 with message “fst”. The agent was started with state 0.
System.InvalidCastException: Specified cast is not valid.
at Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicFunctions.UnboxGeneric[T](Object source)
at FSI_0003.AgentSystem.f@158.Invoke(Object a, Object b)
at Microsoft.FSharp.Core.FastFunc`2.InvokeFast[V](FastFunc`2 func, T arg1, TResult arg2)
at Microsoft.FSharp.Core.FastFunc`2.InvokeFast[V](FastFunc`2 func, T arg1, TResult arg2)
at FSI_0003.AgentSystem.loop@20-3.Invoke(Unit unitVar)
at Microsoft.FSharp.Control.AsyncBuilderImpl.callA@245.Invoke(AsyncParams`1 args)**
You get information about the current state of the agent, the message that generated the error, the initial state of the agent and the exception that was generated. But, in a system with several agents, you’d like to know which one agent failed. Then you need to name your agent:
counter1 <-- SetName("Bob") counter1 <-- "fadfad"
Now you get (important part in blue):
**> The exception below occurred on agent Bob at state 3 with message “fadfad”. The agent was started with state 0.
System.InvalidCastException: Specified cast is not valid.
at Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicFunctions.UnboxGeneric[T](Object source)
at FSI_0003.AgentSystem.f@158.Invoke(Object a, Object b)
at Microsoft.FSharp.Core.FastFunc`2.InvokeFast[V](FastFunc`2 func, T arg1, TResult arg2)
at Microsoft.FSharp.Core.FastFunc`2.InvokeFast[V](FastFunc`2 func, T arg1, TResult arg2)
at FSI_0003.AgentSystem.loop@20-3.Invoke(Unit unitVar)
at Microsoft.FSharp.Control.AsyncBuilderImpl.callA@245.Invoke(AsyncParams`1 args)**
The important thing is that the agent continues running. It lives to fight another day. Hence:
counter1 <-- 3
Produces:
From 3 to 6
Which shows that the agent is running and that it has kept its current state. Also errors can occur inside the message handler with a similar result:
(spawnAgent (fun msg state -> state / msg) 100) <-- 0
Produces:
**> The exception below occurred on agent Undefined at state 100 with message 0. The agent was started with state 100.
System.DivideByZeroException: Attempted to divide by zero.
at FSI_0013.it@48-3.Invoke(Int32 msg, Int32 state)
at Microsoft.FSharp.Core.FastFunc`2.InvokeFast[V](FastFunc`2 func, T arg1, TResult arg2)
at FSI_0003.AgentSystem.f@158.Invoke(Object a, Object b)
at Microsoft.FSharp.Core.FastFunc`2.InvokeFast[V](FastFunc`2 func, T arg1, TResult arg2)
at Microsoft.FSharp.Core.FastFunc`2.InvokeFast[V](FastFunc`2 func, T arg1, TResult arg2)
at FSI_0003.AgentSystem.loop@20-3.Invoke(Unit unitVar)
at Microsoft.FSharp.Control.AsyncBuilderImpl.callA@245.Invoke(AsyncParams`1 args)**
But this might not be what you want. You might want to customize what happens when an error occurs. We’ll talk about that next.
0 Webmentions
These are webmentions via the IndieWeb and webmention.io.
1 Comment
Comments
DotNetShoutout
2009-06-15T09:45:47ZThank you for submitting this cool story - Trackback from DotNetShoutout