2019-03-28 10:52:31 +00:00
|
|
|
import
|
2019-09-10 21:28:38 +00:00
|
|
|
chronos, chronicles
|
2019-04-05 08:13:22 +00:00
|
|
|
|
2020-03-17 16:58:11 +00:00
|
|
|
proc catchOrQuit*(error: Exception) =
|
2019-04-05 08:13:22 +00:00
|
|
|
if error of CatchableError:
|
|
|
|
trace "Async operation ended with a recoverable error", err = error.msg
|
|
|
|
else:
|
2020-02-23 00:53:57 +00:00
|
|
|
fatal "Fatal exception reached", err = error.msg, stackTrace = getStackTrace()
|
2019-04-05 08:13:22 +00:00
|
|
|
quit 1
|
2019-03-28 10:52:31 +00:00
|
|
|
|
|
|
|
proc traceAsyncErrors*(fut: FutureBase) =
|
|
|
|
fut.addCallback do (arg: pointer):
|
|
|
|
if not fut.error.isNil:
|
2019-04-05 08:13:22 +00:00
|
|
|
catchOrQuit fut.error[]
|
2019-03-28 10:52:31 +00:00
|
|
|
|
2019-04-05 08:13:22 +00:00
|
|
|
template traceAwaitErrors*(fut: FutureBase) =
|
|
|
|
let f = fut
|
|
|
|
yield f
|
|
|
|
if not f.error.isNil:
|
|
|
|
catchOrQuit f.error[]
|
2020-06-17 16:14:05 +00:00
|
|
|
|
|
|
|
template awaitWithTimeout*[T](operation: Future[T],
|
|
|
|
deadline: Future[void],
|
|
|
|
onTimeout: untyped): T =
|
|
|
|
let f = operation
|
|
|
|
await f or deadline
|
|
|
|
if not f.finished:
|
|
|
|
cancel f
|
|
|
|
onTimeout
|
|
|
|
else:
|
|
|
|
f.read
|
|
|
|
|
|
|
|
template awaitWithTimeout*[T](operation: Future[T],
|
|
|
|
timeout: Duration,
|
|
|
|
onTimeout: untyped): T =
|
|
|
|
awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)
|
|
|
|
|