From 99d980c3f6016c616edc938ed8d12542fae193b2 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Tue, 6 Jun 2023 22:28:14 +0200 Subject: [PATCH] use correct exception in `parseCmdArg` (#614) `parseCmdArg` is expected to raise `ValueError` but for `enr.Record`, `Node`, `PrivateKey`, and `NatConfig`, we raise `ConfigurationError`. Change to `ValueError` instead. --- tools/dcli.nim | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/tools/dcli.nim b/tools/dcli.nim index e85b44e..c89e634 100644 --- a/tools/dcli.nim +++ b/tools/dcli.nim @@ -1,3 +1,11 @@ +# Copyright (c) 2020-2023 Status Research & Development GmbH +# Licensed under either of +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) +# at your option. +# This file may not be copied, modified, or distributed except according to +# those terms. + import std/[options, strutils, tables, sets], confutils, confutils/std/net, chronicles, chronicles/topics_registry, @@ -104,35 +112,35 @@ func defaultListenAddress*(conf: DiscoveryConf): ValidIpAddress = func defaultAdminListenAddress*(conf: DiscoveryConf): ValidIpAddress = (static ValidIpAddress.init("127.0.0.1")) -proc parseCmdArg*(T: type enr.Record, p: string): T = +proc parseCmdArg*(T: type enr.Record, p: string): T {.raises: [ValueError].} = if not fromURI(result, p): - raise newException(ConfigurationError, "Invalid ENR") + raise newException(ValueError, "Invalid ENR") proc completeCmdArg*(T: type enr.Record, val: string): seq[string] = return @[] -proc parseCmdArg*(T: type Node, p: string): T = +proc parseCmdArg*(T: type Node, p: string): T {.raises: [ValueError].} = var record: enr.Record if not fromURI(record, p): - raise newException(ConfigurationError, "Invalid ENR") + raise newException(ValueError, "Invalid ENR") let n = newNode(record) if n.isErr: - raise newException(ConfigurationError, $n.error) + raise newException(ValueError, $n.error) if n[].address.isNone(): - raise newException(ConfigurationError, "ENR without address") + raise newException(ValueError, "ENR without address") n[] proc completeCmdArg*(T: type Node, val: string): seq[string] = return @[] -proc parseCmdArg*(T: type PrivateKey, p: string): T = +proc parseCmdArg*(T: type PrivateKey, p: string): T {.raises: [ValueError].} = try: result = PrivateKey.fromHex(string(p)).tryGet() except CatchableError: - raise newException(ConfigurationError, "Invalid private key") + raise newException(ValueError, "Invalid private key") proc completeCmdArg*(T: type PrivateKey, val: string): seq[string] = return @[] @@ -168,7 +176,7 @@ proc discover(d: discv5_protocol.Protocol, psFile: string) {.async.} = bits.inc(countOnes(byt.uint)) let str = "$#,$#,$#,$#,$#,$#\n" - let newLine = str % [pubkey.get().toHex, dNode.id.toHex, forkDigest[0..3].toHex, $dNode.address.get(), attnets.get().toHex, $bits] + let newLine = str % [pubkey.get().toHex, dNode.id.toHex, forkDigest[0..3].toHex, $dNode.address.get(), attnets.get().toHex, $bits] ps.write(newLine) await sleepAsync(1000) # 1 sec of delay