diff --git a/libp2p/muxers/mplex/lpchannel.nim b/libp2p/muxers/mplex/lpchannel.nim index f078d371e..9751e1a74 100644 --- a/libp2p/muxers/mplex/lpchannel.nim +++ b/libp2p/muxers/mplex/lpchannel.nim @@ -51,13 +51,16 @@ type resetCode*: MessageType # cached in/out reset code writes*: int # In-flight writes -func shortLog*(s: LPChannel): auto - {.raises: [Defect, ValueError].} = - if s.isNil: "LPChannel(nil)" - elif s.conn.peerInfo.isNil: $s.oid - elif s.name != $s.oid and s.name.len > 0: - &"{shortLog(s.conn.peerInfo.peerId)}:{s.oid}:{s.name}" - else: &"{shortLog(s.conn.peerInfo.peerId)}:{s.oid}" +func shortLog*(s: LPChannel): auto = + try: + if s.isNil: "LPChannel(nil)" + elif s.conn.peerInfo.isNil: $s.oid + elif s.name != $s.oid and s.name.len > 0: + &"{shortLog(s.conn.peerInfo.peerId)}:{s.oid}:{s.name}" + else: &"{shortLog(s.conn.peerInfo.peerId)}:{s.oid}" + except ValueError as exc: + raise newException(Defect, exc.msg) + chronicles.formatIt(LPChannel): shortLog(it) proc open*(s: LPChannel) {.async, gcsafe.} = diff --git a/libp2p/muxers/mplex/mplex.nim b/libp2p/muxers/mplex/mplex.nim index 5ac6e0de6..719e91438 100644 --- a/libp2p/muxers/mplex/mplex.nim +++ b/libp2p/muxers/mplex/mplex.nim @@ -46,9 +46,9 @@ type oid*: Oid maxChannCount: int -func shortLog*(m: MPlex): auto - {.raises: [Defect, ValueError].} = +func shortLog*(m: MPlex): auto = shortLog(m.connection) + chronicles.formatIt(Mplex): shortLog(it) proc newTooManyChannels(): ref TooManyChannels = diff --git a/libp2p/muxers/muxer.nim b/libp2p/muxers/muxer.nim index 49988fd03..0188a0787 100644 --- a/libp2p/muxers/muxer.nim +++ b/libp2p/muxers/muxer.nim @@ -39,8 +39,12 @@ type streamHandler*: StreamHandler # triggered every time there is a new stream, called for any muxer instance muxerHandler*: MuxerHandler # triggered every time there is a new muxed connection created -func shortLog*(m: Muxer): auto {.raises: [Defect, ValueError].} = - shortLog(m.connection) +func shortLog*(m: Muxer): auto = + try: + shortLog(m.connection) + except ValueError as exc: + raise newException(Defect, exc.msg) + chronicles.formatIt(Muxer): shortLog(it) # muxer interface diff --git a/libp2p/protocols/secure/noise.nim b/libp2p/protocols/secure/noise.nim index e5f9c3523..ba327aab3 100644 --- a/libp2p/protocols/secure/noise.nim +++ b/libp2p/protocols/secure/noise.nim @@ -95,11 +95,14 @@ type # Utility -func shortLog*(conn: NoiseConnection): auto - {.raises: [Defect, ValueError].} = - if conn.isNil: "NoiseConnection(nil)" - elif conn.peerInfo.isNil: $conn.oid - else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}" +func shortLog*(conn: NoiseConnection): auto = + try: + if conn.isNil: "NoiseConnection(nil)" + elif conn.peerInfo.isNil: $conn.oid + else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}" + except ValueError as exc: + raise newException(Defect, exc.msg) + chronicles.formatIt(NoiseConnection): shortLog(it) proc genKeyPair(rng: var BrHmacDrbgContext): KeyPair = @@ -591,9 +594,10 @@ method init*(p: Noise) {.gcsafe.} = proc newNoise*( rng: ref BrHmacDrbgContext, - privateKey: PrivateKey; - outgoing: bool = true; - commonPrologue: seq[byte] = @[]): Noise = + privateKey: PrivateKey, + outgoing: bool = true, + commonPrologue: seq[byte] = @[]): Noise + {.raises: [Defect, ResultError[CryptoError]].} = result = Noise( rng: rng, outgoing: outgoing, diff --git a/libp2p/protocols/secure/secio.nim b/libp2p/protocols/secure/secio.nim index b72403677..2a2798b39 100644 --- a/libp2p/protocols/secure/secio.nim +++ b/libp2p/protocols/secure/secio.nim @@ -73,10 +73,14 @@ type SecioError* = object of LPError -func shortLog*(conn: SecioConn): auto {.raises: [Defect, ValueError].} = - if conn.isNil: "SecioConn(nil)" - elif conn.peerInfo.isNil: $conn.oid - else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}" +func shortLog*(conn: SecioConn): auto = + try: + if conn.isNil: "SecioConn(nil)" + elif conn.peerInfo.isNil: $conn.oid + else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}" + except ValueError as exc: + raise newException(Defect, exc.msg) + chronicles.formatIt(SecioConn): shortLog(it) proc init(mac: var SecureMac, hash: string, key: openarray[byte]) = diff --git a/libp2p/protocols/secure/secure.nim b/libp2p/protocols/secure/secure.nim index 78a46c0d6..656dee53d 100644 --- a/libp2p/protocols/secure/secure.nim +++ b/libp2p/protocols/secure/secure.nim @@ -33,10 +33,14 @@ type stream*: Connection buf: StreamSeq -func shortLog*(conn: SecureConn): auto {.raises: [Defect, ValueError].} = - if conn.isNil: "SecureConn(nil)" - elif conn.peerInfo.isNil: $conn.oid - else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}" +func shortLog*(conn: SecureConn): auto = + try: + if conn.isNil: "SecureConn(nil)" + elif conn.peerInfo.isNil: $conn.oid + else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}" + except ValueError as exc: + raise newException(Defect, exc.msg) + chronicles.formatIt(SecureConn): shortLog(it) proc init*(T: type SecureConn, diff --git a/libp2p/stream/bufferstream.nim b/libp2p/stream/bufferstream.nim index e8c7badf2..f78ac6506 100644 --- a/libp2p/stream/bufferstream.nim +++ b/libp2p/stream/bufferstream.nim @@ -35,11 +35,14 @@ type pushedEof*: bool # eof marker has been put on readQueue returnedEof*: bool # 0-byte readOnce has been completed -func shortLog*(s: BufferStream): auto - {.raises: [Defect, ValueError].} = - if s.isNil: "BufferStream(nil)" - elif s.peerInfo.isNil: $s.oid - else: &"{shortLog(s.peerInfo.peerId)}:{s.oid}" +func shortLog*(s: BufferStream): auto = + try: + if s.isNil: "BufferStream(nil)" + elif s.peerInfo.isNil: $s.oid + else: &"{shortLog(s.peerInfo.peerId)}:{s.oid}" + except ValueError as exc: + raise newException(Defect, exc.msg) + chronicles.formatIt(BufferStream): shortLog(it) proc len*(s: BufferStream): int = diff --git a/libp2p/stream/chronosstream.nim b/libp2p/stream/chronosstream.nim index ab4c5618e..0445dfb9e 100644 --- a/libp2p/stream/chronosstream.nim +++ b/libp2p/stream/chronosstream.nim @@ -33,11 +33,14 @@ when defined(libp2p_agents_metrics): declareCounter(libp2p_peers_traffic_read, "incoming traffic", labels = ["agent"]) declareCounter(libp2p_peers_traffic_write, "outgoing traffic", labels = ["agent"]) -func shortLog*(conn: ChronosStream): string - {.raises: [Defect, ValueError].} = - if conn.isNil: "ChronosStream(nil)" - elif conn.peerInfo.isNil: $conn.oid - else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}" +func shortLog*(conn: ChronosStream): auto = + try: + if conn.isNil: "ChronosStream(nil)" + elif conn.peerInfo.isNil: $conn.oid + else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}" + except ValueError as exc: + raise newException(Defect, exc.msg) + chronicles.formatIt(ChronosStream): shortLog(it) method initStream*(s: ChronosStream) = diff --git a/libp2p/stream/connection.nim b/libp2p/stream/connection.nim index dc1d5a3f4..50970a026 100644 --- a/libp2p/stream/connection.nim +++ b/libp2p/stream/connection.nim @@ -56,11 +56,14 @@ proc onUpgrade*(s: Connection) {.async.} = if not isNil(s.upgraded): await s.upgraded -func shortLog*(conn: Connection): string - {.raises: [Defect, ValueError].} = - if conn.isNil: "Connection(nil)" - elif conn.peerInfo.isNil: $conn.oid - else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}" +func shortLog*(conn: Connection): string = + try: + if conn.isNil: "Connection(nil)" + elif conn.peerInfo.isNil: $conn.oid + else: &"{shortLog(conn.peerInfo.peerId)}:{conn.oid}" + except ValueError as exc: + raise newException(Defect, exc.msg) + chronicles.formatIt(Connection): shortLog(it) method initStream*(s: Connection) =