From 50dfe3aab1028fee0e1662a8597e98621a86f3d8 Mon Sep 17 00:00:00 2001 From: gmega Date: Wed, 13 Dec 2023 14:29:27 -0300 Subject: [PATCH] add exception handling shim for Chronos V4 --- asynctest/templates.nim | 23 +++++++++++++++++++---- testmodules/unittest2/test.nimble | 2 +- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/asynctest/templates.nim b/asynctest/templates.nim index 3a338c8..09616eb 100644 --- a/asynctest/templates.nim +++ b/asynctest/templates.nim @@ -1,3 +1,18 @@ +template launderExceptions(body: typed) = + ## Chronos V4 requires that all procs which raise Exception annotate it + ## with {.async: (raises: [Exception]).}, but this construct does not + ## exist in asyncdispatch. We therefore launder all real instances of + ## Exception into CatchableError and make Chronos happy while not losing + ## context information for the remainder of the exception types. + try: + body + except Defect as ex: + raise ex + except CatchableError as ex: + raise ex + except Exception as ex: + raise newException(CatchableError, ex.msg, ex) + template suite*(name, body) = suite name: @@ -10,18 +25,18 @@ template suite*(name, body) = ## Runs after all tests in the suite template teardownAll(teardownAllBody) {.used.} = template teardownAllIMPL: untyped {.inject.} = - let a = proc {.async.} = teardownAllBody + let a = proc {.async.} = launderExceptions: teardownAllBody waitFor a() template setup(setupBody) {.used.} = setup: - let asyncproc = proc {.async.} = setupBody + let asyncproc = proc {.async.} = launderExceptions: setupBody waitFor asyncproc() template teardown(teardownBody) {.used.} = teardown: let exception = getCurrentException() - let asyncproc = proc {.async.} = teardownBody + let asyncproc = proc {.async.} = launderExceptions: teardownBody waitFor asyncproc() setCurrentException(exception) @@ -35,5 +50,5 @@ template suite*(name, body) = template test*(name, body) = test name: - let asyncproc = proc {.async.} = body + let asyncproc = proc {.async.} = launderExceptions: body waitFor asyncproc() diff --git a/testmodules/unittest2/test.nimble b/testmodules/unittest2/test.nimble index f4f5e19..d8533db 100644 --- a/testmodules/unittest2/test.nimble +++ b/testmodules/unittest2/test.nimble @@ -3,7 +3,7 @@ author = "Asynctest Authors" description = "Asynctest tests for pkg/unittest2 and pkg/chronos" license = "MIT" -requires "unittest2" +requires "unittest2 <= 0.0.9" requires "chronos" task test, "Runs the test suite":