Implements getCurrentException for try in async procs. Ref #1487.
This commit is contained in:
parent
6689fa68f5
commit
dd33069d59
3 changed files with 20 additions and 7 deletions
|
|
@ -973,7 +973,7 @@ template createCb*(retFutureSym, iteratorNameSym,
|
||||||
#{.pop.}
|
#{.pop.}
|
||||||
proc generateExceptionCheck(futSym,
|
proc generateExceptionCheck(futSym,
|
||||||
tryStmt, rootReceiver, fromNode: PNimrodNode): PNimrodNode {.compileTime.} =
|
tryStmt, rootReceiver, fromNode: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||||
if tryStmt.len == 1:
|
if tryStmt.kind == nnkNilLit:
|
||||||
result = rootReceiver
|
result = rootReceiver
|
||||||
else:
|
else:
|
||||||
var exceptionChecks: seq[tuple[cond, body: PNimrodNode]] = @[]
|
var exceptionChecks: seq[tuple[cond, body: PNimrodNode]] = @[]
|
||||||
|
|
@ -1007,8 +1007,14 @@ proc generateExceptionCheck(futSym,
|
||||||
let elseNode = newNimNode(nnkElse, fromNode)
|
let elseNode = newNimNode(nnkElse, fromNode)
|
||||||
elseNode.add newNimNode(nnkStmtList, fromNode)
|
elseNode.add newNimNode(nnkStmtList, fromNode)
|
||||||
elseNode[0].add rootReceiver
|
elseNode[0].add rootReceiver
|
||||||
|
|
||||||
|
let ifBody = newStmtList()
|
||||||
|
ifBody.add newCall(newIdentNode("setCurrentException"), errorNode)
|
||||||
|
ifBody.add newIfStmt(exceptionChecks)
|
||||||
|
ifBody.add newCall(newIdentNode("setCurrentException"), newNilLit())
|
||||||
|
|
||||||
result = newIfStmt(
|
result = newIfStmt(
|
||||||
(newDotExpr(futSym, newIdentNode("failed")), newIfStmt(exceptionChecks))
|
(newDotExpr(futSym, newIdentNode("failed")), ifBody)
|
||||||
)
|
)
|
||||||
result.add elseNode
|
result.add elseNode
|
||||||
|
|
||||||
|
|
@ -1224,6 +1230,8 @@ proc recvLine*(socket: TAsyncFD): Future[string] {.async.} =
|
||||||
## If the socket is disconnected in the middle of a line (before ``\r\L``
|
## If the socket is disconnected in the middle of a line (before ``\r\L``
|
||||||
## is read) then line will be set to ``""``.
|
## is read) then line will be set to ``""``.
|
||||||
## The partial line **will be lost**.
|
## The partial line **will be lost**.
|
||||||
|
##
|
||||||
|
## **Warning**: This assumes that lines are delimited by ``\r\l``.
|
||||||
|
|
||||||
template addNLIfEmpty(): stmt =
|
template addNLIfEmpty(): stmt =
|
||||||
if result.len == 0:
|
if result.len == 0:
|
||||||
|
|
@ -1236,9 +1244,8 @@ proc recvLine*(socket: TAsyncFD): Future[string] {.async.} =
|
||||||
if c.len == 0:
|
if c.len == 0:
|
||||||
return ""
|
return ""
|
||||||
if c == "\r":
|
if c == "\r":
|
||||||
c = await recv(socket, 1, {SocketFlag.SafeDisconn, SocketFlag.Peek})
|
c = await recv(socket, 1)
|
||||||
if c.len > 0 and c == "\L":
|
assert c == "\l"
|
||||||
discard await recv(socket, 1)
|
|
||||||
addNLIfEmpty()
|
addNLIfEmpty()
|
||||||
return
|
return
|
||||||
elif c == "\L":
|
elif c == "\L":
|
||||||
|
|
|
||||||
|
|
@ -2630,6 +2630,12 @@ when not defined(JS): #and not defined(NimrodVM):
|
||||||
excHandler.hasRaiseAction = true
|
excHandler.hasRaiseAction = true
|
||||||
excHandler.raiseAction = action
|
excHandler.raiseAction = action
|
||||||
|
|
||||||
|
proc setCurrentException*(exc: ref Exception) {.inline, gcsafe.} =
|
||||||
|
## sets the current exception.
|
||||||
|
##
|
||||||
|
## **Warning**: Only use this if you know what you are doing.
|
||||||
|
currException = exc
|
||||||
|
|
||||||
{.push stack_trace: off, profiler:off.}
|
{.push stack_trace: off, profiler:off.}
|
||||||
when defined(endb) and not defined(NimrodVM):
|
when defined(endb) and not defined(NimrodVM):
|
||||||
include "system/debugger"
|
include "system/debugger"
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ discard """
|
||||||
file: "tasynctry.nim"
|
file: "tasynctry.nim"
|
||||||
exitcode: 0
|
exitcode: 0
|
||||||
output: '''
|
output: '''
|
||||||
Generic except
|
Generic except: Test
|
||||||
Specific except
|
Specific except
|
||||||
Multiple idents in except
|
Multiple idents in except
|
||||||
Multiple except branches
|
Multiple except branches
|
||||||
|
|
@ -22,7 +22,7 @@ proc catch() {.async.} =
|
||||||
try:
|
try:
|
||||||
await foobar()
|
await foobar()
|
||||||
except:
|
except:
|
||||||
echo("Generic except")
|
echo("Generic except: ", getCurrentExceptionMsg())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await foobar()
|
await foobar()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue