mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 05:50:30 +00:00
Implements getCurrentException for try in async procs. Ref #1487.
This commit is contained in:
@@ -973,7 +973,7 @@ template createCb*(retFutureSym, iteratorNameSym,
|
||||
#{.pop.}
|
||||
proc generateExceptionCheck(futSym,
|
||||
tryStmt, rootReceiver, fromNode: PNimrodNode): PNimrodNode {.compileTime.} =
|
||||
if tryStmt.len == 1:
|
||||
if tryStmt.kind == nnkNilLit:
|
||||
result = rootReceiver
|
||||
else:
|
||||
var exceptionChecks: seq[tuple[cond, body: PNimrodNode]] = @[]
|
||||
@@ -1007,8 +1007,14 @@ proc generateExceptionCheck(futSym,
|
||||
let elseNode = newNimNode(nnkElse, fromNode)
|
||||
elseNode.add newNimNode(nnkStmtList, fromNode)
|
||||
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(
|
||||
(newDotExpr(futSym, newIdentNode("failed")), newIfStmt(exceptionChecks))
|
||||
(newDotExpr(futSym, newIdentNode("failed")), ifBody)
|
||||
)
|
||||
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``
|
||||
## is read) then line will be set to ``""``.
|
||||
## The partial line **will be lost**.
|
||||
##
|
||||
## **Warning**: This assumes that lines are delimited by ``\r\l``.
|
||||
|
||||
template addNLIfEmpty(): stmt =
|
||||
if result.len == 0:
|
||||
@@ -1236,9 +1244,8 @@ proc recvLine*(socket: TAsyncFD): Future[string] {.async.} =
|
||||
if c.len == 0:
|
||||
return ""
|
||||
if c == "\r":
|
||||
c = await recv(socket, 1, {SocketFlag.SafeDisconn, SocketFlag.Peek})
|
||||
if c.len > 0 and c == "\L":
|
||||
discard await recv(socket, 1)
|
||||
c = await recv(socket, 1)
|
||||
assert c == "\l"
|
||||
addNLIfEmpty()
|
||||
return
|
||||
elif c == "\L":
|
||||
|
||||
@@ -2630,6 +2630,12 @@ when not defined(JS): #and not defined(NimrodVM):
|
||||
excHandler.hasRaiseAction = true
|
||||
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.}
|
||||
when defined(endb) and not defined(NimrodVM):
|
||||
include "system/debugger"
|
||||
|
||||
@@ -2,7 +2,7 @@ discard """
|
||||
file: "tasynctry.nim"
|
||||
exitcode: 0
|
||||
output: '''
|
||||
Generic except
|
||||
Generic except: Test
|
||||
Specific except
|
||||
Multiple idents in except
|
||||
Multiple except branches
|
||||
@@ -22,7 +22,7 @@ proc catch() {.async.} =
|
||||
try:
|
||||
await foobar()
|
||||
except:
|
||||
echo("Generic except")
|
||||
echo("Generic except: ", getCurrentExceptionMsg())
|
||||
|
||||
try:
|
||||
await foobar()
|
||||
|
||||
Reference in New Issue
Block a user