mirror of https://github.com/status-im/nim-eth.git
Add a new asyncDiscard replacement that traces recoverable errors and aborts on defects
This commit is contained in:
parent
abafbc3a73
commit
1cc52976df
|
@ -0,0 +1,12 @@
|
||||||
|
import
|
||||||
|
chronos/asyncfutures2, chronicles
|
||||||
|
|
||||||
|
proc traceAsyncErrors*(fut: FutureBase) =
|
||||||
|
fut.addCallback do (arg: pointer):
|
||||||
|
if not fut.error.isNil:
|
||||||
|
if fut.error[] of CatchableError:
|
||||||
|
trace "Async operation ended with a recoverable error", err = fut.error.msg
|
||||||
|
else:
|
||||||
|
fatal "Fatal exception reached", err = fut.error.msg
|
||||||
|
quit 1
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
# TODO: Make this part of the test suite.
|
||||||
|
# We need to be able to test that a program fails in certain way.
|
||||||
|
# The testing framework from Chronicles can be extracted in a separate package.
|
||||||
|
|
||||||
|
import
|
||||||
|
chronos, ../eth/async_utils
|
||||||
|
|
||||||
|
type
|
||||||
|
SomeRecoverableError = object of CatchableError
|
||||||
|
SomeDefect = object of Defect
|
||||||
|
|
||||||
|
proc failingAsyncProc(err: ref Exception = nil) {.async.} =
|
||||||
|
await sleepAsync(0)
|
||||||
|
if err != nil:
|
||||||
|
raise err
|
||||||
|
|
||||||
|
proc main {.async.} =
|
||||||
|
type Error =
|
||||||
|
# Exception
|
||||||
|
SomeDefect
|
||||||
|
# SomeRecoverableError
|
||||||
|
traceAsyncErrors failingAsyncProc(newException(Error, "some exception"))
|
||||||
|
|
||||||
|
waitFor main()
|
||||||
|
waitFor sleepAsync(2000)
|
||||||
|
|
Loading…
Reference in New Issue