From a0df7705fba396a018729eac63f49da7b1ab5bfd Mon Sep 17 00:00:00 2001 From: nitely Date: Sun, 22 Dec 2024 12:26:07 -0300 Subject: [PATCH] trycatch --- .gitignore | 1 + src/hyperx/client.nim | 5 +---- src/hyperx/clientserver.nim | 39 ++++++++----------------------------- src/hyperx/server.nim | 29 +++++---------------------- src/hyperx/utils.nim | 34 ++++++++++++++++++++++++-------- 5 files changed, 41 insertions(+), 67 deletions(-) diff --git a/.gitignore b/.gitignore index 107e7e0..1578896 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ examples/dataStream examples/tlsServer config.nims out +patches diff --git a/src/hyperx/client.nim b/src/hyperx/client.nim index e98dd65..6c15289 100644 --- a/src/hyperx/client.nim +++ b/src/hyperx/client.nim @@ -52,14 +52,11 @@ proc defaultSslContext(): SslContext {.raises: [HyperxConnError].} = when not defined(hyperxTest): proc newMySocket(ssl: bool): MyAsyncSocket {.raises: [HyperxConnError].} = result = nil - try: + tryCatch: result = newAsyncSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, buffered = true) doAssert result != nil if ssl: wrapSocket(defaultSslContext(), result) - except CatchableError as err: - debugErr2 err - raise newConnError(err.msg, err) proc newClient*( hostname: string, diff --git a/src/hyperx/clientserver.nim b/src/hyperx/clientserver.nim index 980064b..1c7ef43 100644 --- a/src/hyperx/clientserver.nim +++ b/src/hyperx/clientserver.nim @@ -72,23 +72,12 @@ proc defaultSslContext*( # protSSLv23 will disable all protocols # lower than the min protocol defined # in openssl.config, usually +TLSv1.2 - result = nil - try: - result = newContext( - protSSLv23, - verifyMode = CVerifyPeer, - certFile = certFile, - keyFile = keyFile - ) - except CatchableError as err: - debugErr2 err - raise newConnError(err.msg, err) - except Defect as err: - raise err - except Exception as err: - debugErr2 err - # workaround for newContext raising Exception - raise newException(Defect, err.msg, err) + result = tryCatch newContext( + protSSLv23, + verifyMode = CVerifyPeer, + certFile = certFile, + keyFile = keyFile + ) doAssert result != nil, "failure to initialize the SSL context" # https://httpwg.org/specs/rfc9113.html#tls12features const ctxOps = SSL_OP_ALL or @@ -180,15 +169,7 @@ proc close*(client: ClientContext) {.raises: [HyperxConnError].} = return client.isConnected = false try: - client.sock.close() - except CatchableError as err: - debugErr2 err - raise newConnError(err.msg, err) - except Defect as err: - raise err - except Exception as err: - debugErr2 err - raise newException(Defect, err.msg, err) + tryCatch client.sock.close() finally: client.recvMsgs.close() client.streamOpenedMsgs.close() @@ -741,11 +722,7 @@ proc windowUpdateTask(client: ClientContext) {.async.} = client.close() proc connect(client: ClientContext) {.async.} = - try: - await client.sock.connect(client.hostname, client.port) - except OsError as err: - debugErr2 err - raise newConnError(err.msg, err) + tryCatch await client.sock.connect(client.hostname, client.port) proc failSilently(f: Future[void]) {.async.} = ## Be careful when wrapping non {.async.} procs, diff --git a/src/hyperx/server.nim b/src/hyperx/server.nim index dc1cb60..386ecf6 100644 --- a/src/hyperx/server.nim +++ b/src/hyperx/server.nim @@ -61,14 +61,11 @@ when not defined(hyperxTest): keyFile = "" ): MyAsyncSocket {.raises: [HyperxConnError].} = result = nil - try: + tryCatch: result = newAsyncSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, buffered = true) doAssert result != nil if ssl: wrapSocket(defaultSslContext(certFile, keyFile), result) - except CatchableError as err: - debugErr2 err - raise newConnError(err.msg, err) type ServerContext* = ref object @@ -99,34 +96,21 @@ proc close*(server: ServerContext) {.raises: [HyperxConnError].} = if not server.isConnected: return server.isConnected = false - try: - server.sock.close() - except CatchableError as err: - debugErr2 err - raise newConnError(err.msg, err) - except Defect as err: - raise err - except Exception as err: - debugErr2 err - raise newException(Defect, err.msg, err) + tryCatch server.sock.close() proc listen(server: ServerContext) {.raises: [HyperxConnError].} = - try: + tryCatch: server.sock.setSockOpt(OptReuseAddr, true) server.sock.setSockOpt(OptReusePort, true) server.sock.setSockOpt(OptNoDelay, true, level = IPPROTO_TCP.cint) server.sock.bindAddr server.port server.sock.listen() - except OSError, ValueError: - let err = getCurrentException() - debugErr2 err - raise newConnError(err.msg, err) # XXX dont allow receive push promise # XXX limit number of active clients proc recvClient*(server: ServerContext): Future[ClientContext] {.async.} = - try: + tryCatch: # note OptNoDelay is inherited from server.sock let sock = await server.sock.accept() if server.sock.isSsl: @@ -135,10 +119,7 @@ proc recvClient*(server: ServerContext): Future[ClientContext] {.async.} = wrapConnectedSocket( sslContext, sock, handshakeAsServer, server.hostname ) - result = newClient(ctServer, sock, server.hostname) - except CatchableError as err: - debugErr2 err - raise newConnError(err.msg, err) + return newClient(ctServer, sock, server.hostname) template with*(server: ServerContext, body: untyped): untyped = try: diff --git a/src/hyperx/utils.nim b/src/hyperx/utils.nim index 3e6961b..aeaa020 100644 --- a/src/hyperx/utils.nim +++ b/src/hyperx/utils.nim @@ -68,14 +68,22 @@ template raisesAssertion*(exp: untyped): untyped = template untrackExceptions*(body: untyped): untyped = ## workaround for API errors in Nim's stdlib - {.line: instantiationInfo(fullPaths = true).}: - try: - body - except Defect as err: - raise err # raise original error - except Exception as err: - debugErr err - raise newException(Defect, err.msg, err) + try: + body + except Defect as err: + raise err # raise original error + except Exception as err: + debugErr err + raise newException(Defect, err.msg, err) + +template tryCatch*(body: untyped): untyped = + try: + body + except Defect as err: + raise err + except Exception as err: + debugErr2 err + raise newConnError(err.msg, err) func add*(s: var seq[byte], ss: openArray[char]) {.raises: [].} = let L = s.len @@ -241,6 +249,16 @@ when isMainModule: except AssertionDefect: raised = true doAssert raised + block: + raisesAssertion(tryCatch(doAssert false)) + block: + try: + tryCatch: + raise newException(ValueError, "foo") + doAssert false + except HyperxConnError as err: + doAssert err.msg == "foo" + doAssert err.parent.msg == "foo" block content_len: doAssert contentLen(newSeq[byte]()) == -1 doAssert contentLen(":foo: abc\r\n".toBytes) == -1