Expose coroutine exceptions
Currently, if an exception is raised inside a coroutine, the coroutine will silently fail. Coroutine.is_failed
can let you know that this happened, but I can't see any way to actually retrieve said exception (let me know if I'm wrong about this, though!). Perhaps this "silent fail" behavior is useful in some cases, but as I've started to use coroutines more heavily it's bitten me a few times. It can look like something else in my code is failing at first, and it can sometimes take some investigation to figure out it was a coroutine in the first place; then, after that, I have to add extra debug code to catch the exception inside the coroutine and output the traceback to see what's actually going on.
I assume this behavior is due to coroutines internally being their own VMs. It's not bad in and of itself, but it does cause problems sometimes. I can think of two solutions off the top of my head (not sure which would make more sense):
- Raise exceptions inside coroutines as exceptions in the calling VM. This could maybe be optional, though I'm not sure what a sensible API would look like; if not optional, I could imagine just using try/catch around
Coroutine.resume
in place of whereCoroutine.is_failed
is used right now, but that would be a pretty big breaking change, so perhaps not the best idea. - Add a method for retrieving the exception raised by a coroutine. This could either error out if an exception hasn't been raised, return an
Option[Exception]
, or just raise the exception again in the caller's VM if one exists. I imagine this would be the simpler solution (and also isn't a breaking change); I could maybe try putting together a merge request for something like this if you wanted to go in this direction.
It's possible I'm just putting a bit too much code inside coroutines (I use them to handle scripted events in a game), and this is somewhat of a self-inflicted problem. But, other than this issue, I do find coroutines quite useful, so it would be nice to be able to debug them effectively.