From 0229547c41325ff56d22b401cb6dc2de495831f5 Mon Sep 17 00:00:00 2001 From: kdeme Date: Wed, 4 Dec 2019 12:34:37 +0100 Subject: [PATCH] More explicit Exceptions + fix re-raising --- eth/keyfile/keyfile.nim | 10 +++++----- eth/p2p/discovery.nim | 2 +- eth/p2p/enode.nim | 8 ++++---- eth/p2p/p2p_backends_helpers.nim | 2 +- eth/p2p/peer_pool.nim | 2 +- eth/p2p/rlpx.nim | 6 +++--- tests/p2p/test_rlpx_thunk.nim | 2 +- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/eth/keyfile/keyfile.nim b/eth/keyfile/keyfile.nim index 67cff10..263743b 100644 --- a/eth/keyfile/keyfile.nim +++ b/eth/keyfile/keyfile.nim @@ -231,7 +231,7 @@ proc decodeHex(m: string): seq[byte] = if len(m) > 0: try: result = utils.fromHex(m) - except: + except CatchableError: result = newSeq[byte]() else: result = newSeq[byte]() @@ -243,7 +243,7 @@ proc decodeSalt(m: string): string = sarr = utils.fromHex(m) result = newString(len(sarr)) copyMem(addr result[0], addr sarr[0], len(sarr)) - except: + except CatchableError: result = "" else: result = "" @@ -395,7 +395,7 @@ proc decodeKeyFileJson*(j: JsonNode, return res try: seckey = initPrivateKey(plaintext) - except: + except CatchableError: return IncorrectPrivateKey result = Success else: @@ -414,7 +414,7 @@ proc loadKeyFile*(pathname: string, try: data = parseFile(pathname) result = Success - except: + except CatchableError: result = JsonError finally: stream.close() @@ -432,7 +432,7 @@ proc saveKeyFile*(pathname: string, try: f.write($jobject) result = Success - except: + except CatchableError: result = OsError finally: f.close() diff --git a/eth/p2p/discovery.nim b/eth/p2p/discovery.nim index c1c1998..9451345 100644 --- a/eth/p2p/discovery.nim +++ b/eth/p2p/discovery.nim @@ -269,7 +269,7 @@ proc processClient(transp: DatagramTransport, debug "Receive failed", exc = e.name, err = e.msg except Exception as e: debug "Receive failed", exc = e.name, err = e.msg - raise + raise e proc open*(d: DiscoveryProtocol) = # TODO allow binding to specific IP / IPv6 / etc diff --git a/eth/p2p/enode.nim b/eth/p2p/enode.nim index 79ba93f..8d6029e 100644 --- a/eth/p2p/enode.nim +++ b/eth/p2p/enode.nim @@ -87,7 +87,7 @@ proc initENode*(e: string, node: var ENode): ENodeStatus = tport = parseInt(uri.port) if tport <= 0 or tport > 65535: return IncorrectPort - except: + except ValueError: return IncorrectPort if len(uri.query) > 0: @@ -97,7 +97,7 @@ proc initENode*(e: string, node: var ENode): ENodeStatus = uport = parseInt(uri.query[9..^1]) if uport <= 0 or uport > 65535: return IncorrectDiscPort - except: + except ValueError: return IncorrectDiscPort else: uport = tport @@ -107,12 +107,12 @@ proc initENode*(e: string, node: var ENode): ENodeStatus = if recoverPublicKey(cast[seq[byte]](data), node.pubkey) != EthKeysStatus.Success: return IncorrectNodeId - except: + except CatchableError: return IncorrectNodeId try: node.address.ip = parseIpAddress(uri.hostname) - except: + except ValueError: zeroMem(addr node.pubkey, KeyLength * 2) return IncorrectIP diff --git a/eth/p2p/p2p_backends_helpers.nim b/eth/p2p/p2p_backends_helpers.nim index db987a7..5e8f08d 100644 --- a/eth/p2p/p2p_backends_helpers.nim +++ b/eth/p2p/p2p_backends_helpers.nim @@ -73,7 +73,7 @@ proc requestResolver[MsgType](msg: pointer, future: FutureBase) {.gcsafe.} = trace "Transport got closed during request" except Exception as e: debug "Exception in requestResolver()", exc = e.name, err = e.msg - raise + raise e proc linkSendFailureToReqFuture[S, R](sendFut: Future[S], resFut: Future[R]) = sendFut.addCallback() do (arg: pointer): diff --git a/eth/p2p/peer_pool.nim b/eth/p2p/peer_pool.nim index 3aa7d2a..d1b7c5e 100644 --- a/eth/p2p/peer_pool.nim +++ b/eth/p2p/peer_pool.nim @@ -169,7 +169,7 @@ proc run(p: PeerPool) {.async.} = var dropConnections = false try: await p.maybeConnectToMorePeers() - except Exception as e: + except CatchableError as e: # Most unexpected errors should be transient, so we log and restart from # scratch. error "Unexpected PeerPool error, restarting", diff --git a/eth/p2p/rlpx.nim b/eth/p2p/rlpx.nim index a19c38e..75e3839 100644 --- a/eth/p2p/rlpx.nim +++ b/eth/p2p/rlpx.nim @@ -270,9 +270,9 @@ proc sendMsg*(peer: Peer, data: Bytes) {.gcsafe, async.} = if res != len(cipherText): # This is ECONNRESET or EPIPE case when remote peer disconnected. await peer.disconnect(TcpError) - except: + except CatchableError as e: await peer.disconnect(TcpError) - raise + raise e proc send*[Msg](peer: Peer, msg: Msg): Future[void] = logSentMsg(peer, msg) @@ -450,7 +450,7 @@ proc checkedRlpRead(peer: Peer, r: var Rlp, MsgType: type): auto {.inline.} = exception = e.msg # rlpData = r.inspect - raise + raise e proc waitSingleMsg(peer: Peer, MsgType: type): Future[MsgType] {.async.} = let wantedId = peer.perPeerMsgId(MsgType) diff --git a/tests/p2p/test_rlpx_thunk.nim b/tests/p2p/test_rlpx_thunk.nim index 8955bde..1a86a22 100644 --- a/tests/p2p/test_rlpx_thunk.nim +++ b/tests/p2p/test_rlpx_thunk.nim @@ -49,6 +49,6 @@ proc testPayloads(filename: string) = testThunk(payload) except CatchableError as e: check: e.name == error.str - raise + raise e testPayloads(sourceDir / "test_rlpx_thunk.json")