fix: no defects (#163)

This commit is contained in:
vladopajic
2025-10-21 19:39:10 +02:00
committed by GitHub
parent 525842aeca
commit 78ab34c68a
10 changed files with 54 additions and 53 deletions

View File

@@ -64,12 +64,15 @@ proc waitClosed*(connection: Connection) {.async: (raises: [CancelledError]).} =
proc startSending(connection: Connection, remote: TransportAddress) =
trace "Starting sending loop"
proc onStop(e: ref CatchableError) {.async.} =
proc onStop(e: ref CatchableError) {.async: (raises: []).} =
if not connection.loop.finished:
connection.loop.fail(e)
await connection.drop()
try:
await connection.drop()
except CatchableError as e:
trace "Failed to drop connection", msg = e.msg
proc send() {.async.} =
proc send() {.async: (raises: [CancelledError]).} =
try:
let datagram = await connection.quic.outgoing.get()
await connection.udp.sendTo(remote, datagram.data)

View File

@@ -1,11 +1,4 @@
type
QuicDefect* = object of Defect
QuicConfigError* = object of CatchableError
QuicError* = object of IOError
ClosedStreamError* = object of QuicError
template errorAsDefect*(body): untyped =
try:
body
except CatchableError as error:
raise (ref QuicDefect)(msg: error.msg, parent: error)

View File

@@ -1,15 +1,11 @@
import pkg/chronos
proc asyncLoop*(
repeat: proc(): Future[void] {.gcsafe, raises: [CatchableError].}
): Future[void] {.async.} =
repeat: proc(): Future[void].Raising([CancelledError]) {.gcsafe, raises: [].}
): Future[void] {.async: (raises: [CancelledError]).} =
## Repeatedly calls the async proc `repeat` until cancelled.
## Any `Error`s that are raised in `repeat` are considered fatal and therefore
## re-raised as `Defect`s.
while true:
try:
await repeat()
except CancelledError as exc:
raise exc
except CatchableError as error:
raise newException(FutureDefect, error.msg)
except CancelledError as e:
raise e

View File

@@ -48,9 +48,11 @@ method enter(
state.ngtcp2Connection.onNewId = Opt.some(onNewId)
state.ngtcp2Connection.onRemoveId = Opt.some(onRemoveId)
state.ngtcp2Connection.onSend = proc(datagram: Datagram) =
errorAsDefect:
state.ngtcp2Connection.onSend = proc(datagram: Datagram) {.raises: [QuicError].} =
try:
connection.outgoing.putNoWait(datagram)
except AsyncQueueFullError:
raise newException(QuicError, "Outgoing queue is full")
state.ngtcp2Connection.onIncomingStream = proc(stream: Stream) =
state.streams.add(stream)

View File

@@ -216,7 +216,7 @@ proc handleTimeout(connection: Ngtcp2Connection) =
let conn = connection.conn.valueOr:
return
errorAsDefect:
try:
let ret = ngtcp2_conn_handle_expiry(conn, now())
trace "handleExpiry", code = ret
if ret == NGTCP2_ERR_IDLE_CLOSE:
@@ -225,6 +225,8 @@ proc handleTimeout(connection: Ngtcp2Connection) =
else:
checkResult ret
connection.send()
except QuicError as e:
error "handleTimeout unexpected error", msg = e.msg
proc close*(connection: Ngtcp2Connection): Datagram =
let conn = connection.conn.valueOr:

View File

@@ -1,19 +1,24 @@
import ngtcp2
import chronicles
import ../../../errors
type
Ngtcp2Error* = ref object of QuicError
code*: cint
logScope:
topics = "ngtcp2 error"
Ngtcp2Defect* = object of QuicDefect
type Ngtcp2Error* = ref object of QuicError
code*: cint
isFatal*: bool
proc checkResult*(result: cint) =
if result < 0:
let msg = $ngtcp2_strerror(result)
if ngtcp2_err_is_fatal(result) != 0:
raise newException(Ngtcp2Defect, msg)
else:
let e = new(Ngtcp2Error)
e.code = result
e.msg = msg
raise e
proc checkResult*(result: cint) {.raises: [Ngtcp2Error].} =
if result >= 0:
return
let e = new(Ngtcp2Error)
e.code = result
e.isFatal = ngtcp2_err_is_fatal(result) != 0
e.msg = $ngtcp2_strerror(result)
if e.isFatal:
error "Created fatal error", code = e.code, msg = e.msg
raise e

View File

@@ -28,7 +28,7 @@ type
rng*: ref HmacDrbgContext
flowing*: AsyncEvent
expiryTimer*: Timeout
onSend*: proc(datagram: Datagram) {.gcsafe, raises: [].}
onSend*: proc(datagram: Datagram) {.gcsafe, raises: [QuicError].}
onTimeout*: proc() {.gcsafe, raises: [].}
onIncomingStream*: proc(stream: Stream)
onHandshakeDone*: proc()