From 4976bd9fb95c36df3688867a4e2fe7bbfed8f966 Mon Sep 17 00:00:00 2001 From: kdeme Date: Mon, 2 Dec 2019 16:31:10 +0100 Subject: [PATCH] Replace getCurrentException and getCurrentExceptionMsg --- eth/net/nat.nim | 8 ++--- eth/p2p/blockchain_sync.nim | 18 ++++------- eth/p2p/discovery.nim | 10 +++--- eth/p2p/p2p_backends_helpers.nim | 10 +++--- eth/p2p/peer_pool.nim | 3 +- eth/p2p/rlpx.nim | 34 +++++++++------------ eth/p2p/rlpx_protocols/les/flow_control.nim | 5 ++- tests/fuzzing/discovery/fuzz.nim | 4 +-- tests/fuzzing/fuzztest.nim | 3 +- tests/fuzzing/readme.md | 4 +-- tests/fuzzing/rlp/rlp_inspect.nim | 4 +-- 11 files changed, 46 insertions(+), 57 deletions(-) diff --git a/eth/net/nat.nim b/eth/net/nat.nim index a13f012..40d1dde 100644 --- a/eth/net/nat.nim +++ b/eth/net/nat.nim @@ -71,8 +71,8 @@ proc getExternalIP*(natStrategy: NatStrategy, quiet = false): Option[IpAddress] externalIP = parseIpAddress(ires.value) strategy = NatUpnp return some(externalIP) - except: - error "parseIpAddress() exception", err = getCurrentExceptionMsg() + except ValueError as e: + error "parseIpAddress() exception", err = e.msg return if natStrategy == NatAny or natStrategy == NatPmp: @@ -89,8 +89,8 @@ proc getExternalIP*(natStrategy: NatStrategy, quiet = false): Option[IpAddress] externalIP = parseIpAddress($(nires.value)) strategy = NatPmp return some(externalIP) - except: - error "parseIpAddress() exception", err = getCurrentExceptionMsg() + except ValueError as e: + error "parseIpAddress() exception", err = e.msg return proc doPortMapping(tcpPort, udpPort: Port, description: string): Option[(Port, Port)] {.gcsafe.} = diff --git a/eth/p2p/blockchain_sync.nim b/eth/p2p/blockchain_sync.nim index 6e4f7a6..da0eceb 100644 --- a/eth/p2p/blockchain_sync.nim +++ b/eth/p2p/blockchain_sync.nim @@ -198,10 +198,8 @@ proc obtainBlocksFromPeer(syncCtx: SyncContext, peer: Peer) {.async.} = syncCtx.endBlockNumber = bestBlockNumber except TransportError: debug "Transport got closed during obtainBlocksFromPeer" - except CatchableError: - debug "Exception in getBestBlockNumber()", - exc = getCurrentException().name, - err = getCurrentExceptionMsg() + except CatchableError as e: + debug "Exception in getBestBlockNumber()", exc = e.name, err = e.msg # no need to exit here, because the context might still have blocks to fetch # from this peer @@ -252,13 +250,11 @@ proc obtainBlocksFromPeer(syncCtx: SyncContext, peer: Peer) {.async.} = warn "Bodies len != headers.len", bodies = bodies.len, headers = workItem.headers.len except TransportError: debug "Transport got closed during obtainBlocksFromPeer" - except CatchableError: + except CatchableError as e: # the success case sets `dataReceived`, so we can just fall back to the # failure path below. If we signal time-outs with exceptions such # failures will be easier to handle. - debug "Exception in obtainBlocksFromPeer()", - exc = getCurrentException().name, - err = getCurrentExceptionMsg() + debug "Exception in obtainBlocksFromPeer()", exc = e.name, err = e.msg var giveUpOnPeer = false @@ -365,10 +361,8 @@ proc onPeerConnected(ctx: SyncContext, peer: Peer) = error "startSyncWithPeer failed", msg = f.readError.msg, peer except TransportError: debug "Transport got closed during startSyncWithPeer" - except CatchableError: - debug "Exception in startSyncWithPeer()", - exc = getCurrentException().name, - err = getCurrentExceptionMsg() + except CatchableError as e: + debug "Exception in startSyncWithPeer()", exc = e.name, err = e.msg proc onPeerDisconnected(ctx: SyncContext, p: Peer) = diff --git a/eth/p2p/discovery.nim b/eth/p2p/discovery.nim index 0bfdbc0..c1c1998 100644 --- a/eth/p2p/discovery.nim +++ b/eth/p2p/discovery.nim @@ -263,10 +263,12 @@ proc processClient(transp: DatagramTransport, var buf = transp.getMessage() let a = Address(ip: raddr.address, udpPort: raddr.port, tcpPort: raddr.port) proto.receive(a, buf) - except RlpError, DiscProtocolError: - debug "Receive failed", err = getCurrentExceptionMsg() - except: - debug "Receive failed", err = getCurrentExceptionMsg() + except RlpError as e: + debug "Receive failed", exc = e.name, err = e.msg + except DiscProtocolError as e: + debug "Receive failed", exc = e.name, err = e.msg + except Exception as e: + debug "Receive failed", exc = e.name, err = e.msg raise proc open*(d: DiscoveryProtocol) = diff --git a/eth/p2p/p2p_backends_helpers.nim b/eth/p2p/p2p_backends_helpers.nim index b851a95..db987a7 100644 --- a/eth/p2p/p2p_backends_helpers.nim +++ b/eth/p2p/p2p_backends_helpers.nim @@ -66,15 +66,13 @@ proc requestResolver[MsgType](msg: pointer, future: FutureBase) {.gcsafe.} = # This can except when the future still completes with an error. # E.g. the `sendMsg` fails because of an already closed transport or a # broken pipe - except TransportOsError: + except TransportOsError as e: # E.g. broken pipe - trace "TransportOsError during request", err = getCurrentExceptionMsg() + trace "TransportOsError during request", err = e.msg except TransportError: trace "Transport got closed during request" - except: - debug "Exception in requestResolver()", - exc = getCurrentException().name, - err = getCurrentExceptionMsg() + except Exception as e: + debug "Exception in requestResolver()", exc = e.name, err = e.msg raise proc linkSendFailureToReqFuture[S, R](sendFut: Future[S], resFut: Future[R]) = diff --git a/eth/p2p/peer_pool.nim b/eth/p2p/peer_pool.nim index 3bd1bc5..3aa7d2a 100644 --- a/eth/p2p/peer_pool.nim +++ b/eth/p2p/peer_pool.nim @@ -173,8 +173,7 @@ proc run(p: PeerPool) {.async.} = # Most unexpected errors should be transient, so we log and restart from # scratch. error "Unexpected PeerPool error, restarting", - err = getCurrentExceptionMsg(), - stackTrace = e.getStackTrace() + err = e.msg, stackTrace = e.getStackTrace() dropConnections = true if dropConnections: diff --git a/eth/p2p/rlpx.nim b/eth/p2p/rlpx.nim index 4fbcdef..a19c38e 100644 --- a/eth/p2p/rlpx.nim +++ b/eth/p2p/rlpx.nim @@ -443,11 +443,11 @@ proc checkedRlpRead(peer: Peer, r: var Rlp, MsgType: type): auto {.inline.} = else: try: return r.read(MsgType) - except: + except Exception as e: debug "Failed rlp.read", peer = peer, dataType = MsgType.name, - exception = getCurrentExceptionMsg() + exception = e.msg # rlpData = r.inspect raise @@ -533,10 +533,9 @@ proc dispatchMessages*(peer: Peer) {.async.} = msg = peer.getMsgName(msgId) await peer.disconnect(BreachOfProtocol, true) return - except CatchableError: + except CatchableError as e: warn "Error while handling RLPx message", peer, - msg = peer.getMsgName(msgId), - err = getCurrentExceptionMsg() + msg = peer.getMsgName(msgId), err = e.msg # TODO: Hmm, this can be safely moved into the message handler thunk. # The documentation will need to be updated, explaning the fact that @@ -546,12 +545,12 @@ proc dispatchMessages*(peer: Peer) {.async.} = let msgInfo = peer.dispatcher.messages[msgId] try: (msgInfo.nextMsgResolver)(msgData, peer.awaitedMessages[msgId]) - except: + except CatchableError as e: # TODO: Handling errors here must be investigated more carefully. # They also are supposed to be handled at the call-site where # `nextMsg` is used. debug "nextMsg resolver failed, ending dispatchMessages loop", peer, - err = getCurrentExceptionMsg() + err = e.msg await peer.disconnect(BreachOfProtocol, true) return peer.awaitedMessages[msgId] = nil @@ -1044,12 +1043,11 @@ proc rlpxConnect*(node: EthereumNode, remote: Node): Future[Peer] {.async.} = # Some peers report capabilities with names longer than 3 chars. We ignore # those for now. Maybe we should allow this though. debug "Rlp error in rlpxConnect" - except TransportOsError: - trace "TransportOsError", err = getCurrentExceptionMsg() - except CatchableError: - error "Unexpected exception in rlpxConnect", remote, - exc = getCurrentException().name, - err = getCurrentExceptionMsg() + except TransportOsError as e: + trace "TransportOsError", err = e.msg + except CatchableError as e: + error "Unexpected exception in rlpxConnect", remote, exc = e.name, + err = e.msg if not ok: if not isNil(result.transport): @@ -1152,12 +1150,10 @@ proc rlpxAccept*(node: EthereumNode, # Some peers report capabilities with names longer than 3 chars. We ignore # those for now. Maybe we should allow this though. debug "Rlp error in rlpxAccept" - except TransportOsError: - trace "TransportOsError", err = getCurrentExceptionMsg() - except CatchableError: - error "Unexpected exception in rlpxAccept", - exc = getCurrentException().name, - err = getCurrentExceptionMsg() + except TransportOsError as e: + trace "TransportOsError", err = e.msg + except CatchableError as e: + error "Unexpected exception in rlpxAccept", exc = e.name, err = e.msg if not ok: if not isNil(result.transport): diff --git a/eth/p2p/rlpx_protocols/les/flow_control.nim b/eth/p2p/rlpx_protocols/les/flow_control.nim index b96e1ac..92ad5cf 100644 --- a/eth/p2p/rlpx_protocols/les/flow_control.nim +++ b/eth/p2p/rlpx_protocols/les/flow_control.nim @@ -118,9 +118,8 @@ proc loadMessageStats*(network: LesNetwork, return true - except RlpError: - error "Error while loading LES message stats", - err = getCurrentExceptionMsg() + except RlpError as e: + error "Error while loading LES message stats", err = e.msg newSeq(network.messageStats, les.messages[^1].id + 1) return false diff --git a/tests/fuzzing/discovery/fuzz.nim b/tests/fuzzing/discovery/fuzz.nim index 2ff232c..c7237cb 100644 --- a/tests/fuzzing/discovery/fuzz.nim +++ b/tests/fuzzing/discovery/fuzz.nim @@ -30,5 +30,5 @@ test: targetNode.receive(address, msg) # These errors are also catched in `processClient` in discovery.nim # TODO: move them a layer down in discovery so we can do a cleaner test there? - except RlpError, DiscProtocolError: - debug "Receive failed", err = getCurrentExceptionMsg() \ No newline at end of file + except RlpError, DiscProtocolError as e: + debug "Receive failed", err = e.msg \ No newline at end of file diff --git a/tests/fuzzing/fuzztest.nim b/tests/fuzzing/fuzztest.nim index 232b12d..1ad131b 100644 --- a/tests/fuzzing/fuzztest.nim +++ b/tests/fuzzing/fuzztest.nim @@ -7,7 +7,8 @@ template fuzz(body) = try: body except Exception as e: - error "Fuzzer input created exception", exception=e.name, trace=e.repr, msg=e.msg + error "Fuzzer input created exception", exception=e.name, trace=e.repr, + msg=e.msg discard kill(getpid(), SIGSEGV) else: body diff --git a/tests/fuzzing/readme.md b/tests/fuzzing/readme.md index 2e29df0..83cc3fa 100644 --- a/tests/fuzzing/readme.md +++ b/tests/fuzzing/readme.md @@ -32,8 +32,8 @@ test: try: var rlp = rlpFromBytes(@payload.toRange) discard rlp.inspect() - except RlpError: - debug "Inspect failed", err = getCurrentExceptionMsg() + except RlpError as e: + debug "Inspect failed", err = e.msg ``` ## Supported Fuzzers diff --git a/tests/fuzzing/rlp/rlp_inspect.nim b/tests/fuzzing/rlp/rlp_inspect.nim index 44a7074..ddb639e 100644 --- a/tests/fuzzing/rlp/rlp_inspect.nim +++ b/tests/fuzzing/rlp/rlp_inspect.nim @@ -4,5 +4,5 @@ test: try: var rlp = rlpFromBytes(@payload.toRange) discard rlp.inspect() - except RlpError: - debug "Inspect failed", err = getCurrentExceptionMsg() + except RlpError as e: + debug "Inspect failed", err = e.msg