Fix websocket EOF reading exception (#689)

This commit is contained in:
Tanguy 2022-01-28 19:05:07 +01:00 committed by GitHub
parent c18830ad33
commit 07da14a7a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 5 deletions

View File

@ -49,11 +49,24 @@ proc new*(T: type WsStream,
stream.initStream()
return stream
template mapExceptions(body: untyped) =
try:
body
except AsyncStreamIncompleteError:
raise newLPStreamEOFError()
except AsyncStreamUseClosedError:
raise newLPStreamEOFError()
except WSClosedError:
raise newLPStreamEOFError()
except AsyncStreamLimitError:
raise newLPStreamLimitError()
method readOnce*(
s: WsStream,
pbytes: pointer,
nbytes: int): Future[int] {.async.} =
let res = await s.session.recv(pbytes, nbytes)
let res = mapExceptions(await s.session.recv(pbytes, nbytes))
if res == 0 and s.session.readyState == ReadyState.Closed:
raise newLPStreamEOFError()
return res
@ -61,10 +74,7 @@ method readOnce*(
method write*(
s: WsStream,
msg: seq[byte]): Future[void] {.async.} =
try:
await s.session.send(msg, Opcode.Binary)
except WSClosedError:
raise newLPStreamEOFError()
mapExceptions(await s.session.send(msg, Opcode.Binary))
method closeImpl*(s: WsStream): Future[void] {.async.} =
await s.session.close()