Hi, I have a few asynchronous functions (marked {.async.}). Since they may throw exceptions, I would like to put a catch-all handler that makes the whole process not crash.
Unfortunately, if I try to do
try:
await something
I get Error: 'yield' cannot be used within 'try' in a non-inlined iterator.
Is there a way to catch exceptions raised inside an asynchronous block?
If you check asyncdispatch.nim code you will see this lines:
## Limitations/Bugs
## ----------------
##
## * The effect system (``raises: []``) does not work with async procedures.
## * Can't await in a ``except`` body
## * Forward declarations for async procs are broken,
## link includes workaround: https://github.com/nim-lang/Nim/issues/3182.
## * FutureVar[T] needs to be completed manually.
So we need to wait for Dominik.
The support for the try statement in async procedures is still very unstable unfortunately. The reason for this is that you cannot yield inside a try statement so the async macro attempts to transform try statements in a way that ensures yields are performed outside of it. This support works only for simple cases and in fact does not match the semantics for try statements perfectly.
You may wish to instead do something like the following:
var fut = asyncProc()
await fut
if fut.failed:
echo("The async proc raised an error")
Hi @dom96, unfortunately this does not work anymore.
I was using this in rosencrantz to catch exceptions and return an error 500 instead of crashing the whole process (see here), but today it looks like this was broken.
The error appeared between this commit and this one