So I have two classes:
IOPin & Node
A Node has a set of IOPins.
To support loose coupling, and separation of concerns, I don't want the IOPins to have to know anything about Node. The Node should deal with adding/removing IOPins and should also deal with any error checking logic associated with this.
This causes an issue when I want to do error checking when connecting IOPins together:
When two pins are 'connected' they fire a 'connection changed' event. The Node which initialised these IOPins hears this change and checks to see if an IOPin is being connected to another IOPin on the same Node, which is not allowed.
If the Node discovers it owns both IOPins, it tells one of the IOPins to fire off a 'connection not allowed' exception by calling a function which is used for other cases where a connection is not allowed (for example, connecting an input to an input).
This works fine in terms of preventing bad connections, but becasuse of the asynchonous nature of the error checking I'm having trouble dealing with this in my unit tests: Even if I wrap the bad 'connect' call in try catch, it won't catch the error because it occurs asynchronously.
Edit: Additionally, this is going to cause issues too if I want to ever catch & handle that exception without the program crashing... I guess this is the reason people complain about the lack of global exception handling in actionscript?
Should I start to think about using Error Events instead?
What's the best way to handle 'asynchronous' errors?
Thanks.
Edit: I'm solving this specific problem by implementing the Mediator pattern, but I'm still curious as to the answer.