Skip to content

Commit

Permalink
Bumping vendor/nim-confutils and vendor/nim-serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivansete-status committed Sep 21, 2023
1 parent 5f98964 commit c2e905e
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 38 deletions.
8 changes: 4 additions & 4 deletions apps/chat2/config_chat2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ proc parseCmdArg*(T: type crypto.PrivateKey, p: string): T =
# XXX: Here at the moment
result = crypto.PrivateKey(scheme: Secp256k1, skkey: key)
except CatchableError as e:
raise newException(ConfigurationError, "Invalid private key")
raise newException(ValueError, "Invalid private key")

proc completeCmdArg*(T: type crypto.PrivateKey, val: string): seq[string] =
return @[]
Expand All @@ -282,7 +282,7 @@ proc parseCmdArg*(T: type ValidIpAddress, p: string): T =
try:
result = ValidIpAddress.init(p)
except CatchableError as e:
raise newException(ConfigurationError, "Invalid IP address")
raise newException(ValueError, "Invalid IP address")

proc completeCmdArg*(T: type ValidIpAddress, val: string): seq[string] =
return @[]
Expand All @@ -291,7 +291,7 @@ proc parseCmdArg*(T: type Port, p: string): T =
try:
result = Port(parseInt(p))
except CatchableError as e:
raise newException(ConfigurationError, "Invalid Port number")
raise newException(ValueError, "Invalid Port number")

proc completeCmdArg*(T: type Port, val: string): seq[string] =
return @[]
Expand All @@ -300,7 +300,7 @@ proc parseCmdArg*(T: type Option[uint], p: string): T =
try:
some(parseUint(p))
except CatchableError:
raise newException(ConfigurationError, "Invalid unsigned integer")
raise newException(ValueError, "Invalid unsigned integer")

func defaultListenAddress*(conf: Chat2Conf): ValidIpAddress =
# TODO: How should we select between IPv4 and IPv6
Expand Down
6 changes: 3 additions & 3 deletions apps/chat2bridge/config_chat2bridge.nim
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ proc parseCmdArg*(T: type keys.KeyPair, p: string): T =
let privkey = keys.PrivateKey.fromHex(string(p)).tryGet()
result = privkey.toKeyPair()
except CatchableError:
raise newException(ConfigurationError, "Invalid private key")
raise newException(ValueError, "Invalid private key")

proc completeCmdArg*(T: type keys.KeyPair, val: string): seq[string] =
return @[]
Expand All @@ -144,7 +144,7 @@ proc parseCmdArg*(T: type crypto.PrivateKey, p: string): T =
if key.isOk():
crypto.PrivateKey(scheme: Secp256k1, skkey: key.get())
else:
raise newException(ConfigurationError, "Invalid private key")
raise newException(ValueError, "Invalid private key")

proc completeCmdArg*(T: type crypto.PrivateKey, val: string): seq[string] =
return @[]
Expand All @@ -153,7 +153,7 @@ proc parseCmdArg*(T: type ValidIpAddress, p: string): T =
try:
result = ValidIpAddress.init(p)
except CatchableError:
raise newException(ConfigurationError, "Invalid IP address")
raise newException(ValueError, "Invalid IP address")

proc completeCmdArg*(T: type ValidIpAddress, val: string): seq[string] =
return @[]
Expand Down
4 changes: 2 additions & 2 deletions apps/networkmonitor/networkmonitor_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ proc parseCmdArg*(T: type ValidIpAddress, p: string): T =
try:
result = ValidIpAddress.init(p)
except CatchableError as e:
raise newException(ConfigurationError, "Invalid IP address")
raise newException(ValueError, "Invalid IP address")

proc completeCmdArg*(T: type ValidIpAddress, val: string): seq[string] =
return @[]
Expand All @@ -78,7 +78,7 @@ proc parseCmdArg*(T: type chronos.Duration, p: string): T =
try:
result = chronos.seconds(parseInt(p))
except CatchableError as e:
raise newException(ConfigurationError, "Invalid duration value")
raise newException(ValueError, "Invalid duration value")

proc completeCmdArg*(T: type chronos.Duration, val: string): seq[string] =
return @[]
Expand Down
2 changes: 1 addition & 1 deletion apps/wakucanary/wakucanary.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ proc parseCmdArg*(T: type chronos.Duration, p: string): T =
try:
result = chronos.seconds(parseInt(p))
except CatchableError:
raise newException(ConfigurationError, "Invalid timeout value")
raise newException(ValueError, "Invalid timeout value")

proc completeCmdArg*(T: type chronos.Duration, val: string): seq[string] =
return @[]
Expand Down
20 changes: 9 additions & 11 deletions apps/wakunode2/external_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export
confEnvvarDefs,
confEnvvarNet


type ConfResult*[T] = Result[T, string]
type ProtectedTopic* = object
topic*: string
Expand Down Expand Up @@ -456,19 +455,19 @@ proc parseCmdArg*(T: type crypto.PrivateKey, p: string): T =
let key = SkPrivateKey.init(utils.fromHex(p)).tryGet()
crypto.PrivateKey(scheme: Secp256k1, skkey: key)
except CatchableError:
raise newException(ConfigurationError, "Invalid private key")
raise newException(ValueError, "Invalid private key")

proc completeCmdArg*(T: type crypto.PrivateKey, val: string): seq[string] =
return @[]

proc parseCmdArg*(T: type ProtectedTopic, p: string): T =
let elements = p.split(":")
if elements.len != 2:
raise newException(ConfigurationError, "Invalid format for protected topic expected topic:publickey")
raise newException(ValueError, "Invalid format for protected topic expected topic:publickey")

let publicKey = secp256k1.SkPublicKey.fromHex(elements[1])
if publicKey.isErr:
raise newException(ConfigurationError, "Invalid public key")
raise newException(ValueError, "Invalid public key")

return ProtectedTopic(topic: elements[0], key: publicKey.get())

Expand All @@ -479,7 +478,7 @@ proc parseCmdArg*(T: type ValidIpAddress, p: string): T =
try:
ValidIpAddress.init(p)
except CatchableError:
raise newException(ConfigurationError, "Invalid IP address")
raise newException(ValueError, "Invalid IP address")

proc completeCmdArg*(T: type ValidIpAddress, val: string): seq[string] =
return @[]
Expand All @@ -489,12 +488,11 @@ proc defaultListenAddress*(): ValidIpAddress =
# Maybe there should be a config option for this.
(static ValidIpAddress.init("0.0.0.0"))


proc parseCmdArg*(T: type Port, p: string): T =
try:
Port(parseInt(p))
except CatchableError:
raise newException(ConfigurationError, "Invalid Port number")
raise newException(ValueError, "Invalid Port number")

proc completeCmdArg*(T: type Port, val: string): seq[string] =
return @[]
Expand All @@ -503,13 +501,13 @@ proc parseCmdArg*(T: type Option[int], p: string): T =
try:
some(parseInt(p))
except CatchableError:
raise newException(ConfigurationError, "Invalid number")
raise newException(ValueError, "Invalid number")

proc parseCmdArg*(T: type Option[uint], p: string): T =
try:
some(parseUint(p))
except CatchableError:
raise newException(ConfigurationError, "Invalid unsigned integer")
raise newException(ValueError, "Invalid unsigned integer")

## Configuration validation

Expand All @@ -531,7 +529,6 @@ proc readValue*(r: var TomlReader, value: var crypto.PrivateKey) {.raises: [Seri
except CatchableError:
raise newException(SerializationError, getCurrentExceptionMsg())


proc readValue*(r: var EnvvarReader, value: var crypto.PrivateKey) {.raises: [SerializationError].} =
try:
value = parseCmdArg(crypto.PrivateKey, r.readValue(string))
Expand All @@ -556,7 +553,8 @@ proc load*(T: type WakuNodeConf, version=""): ConfResult[T] =
try:
let conf = WakuNodeConf.load(
version=version,
secondarySources = proc (conf: WakuNodeConf, sources: auto) =
secondarySources = proc (conf: WakuNodeConf, sources: auto)
{.gcsafe, raises: [ConfigurationError].} =
sources.addConfigFile(Envvar, InputFile("wakunode2"))

if conf.configFile.isSome():
Expand Down
8 changes: 3 additions & 5 deletions tests/common/test_confutils_envvar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import
../../waku/common/confutils/envvar/defs as confEnvvarDefs,
../../waku/common/confutils/envvar/std/net as confEnvvarNet


type ConfResult[T] = Result[T, string]

type TestConf = object
Expand All @@ -34,13 +33,13 @@ type TestConf = object
defaultValue: 60000,
name: "tcp-port" }: Port


{.push warning[ProveInit]: off.}

proc load*(T: type TestConf, prefix: string): ConfResult[T] =
try:
let conf = TestConf.load(
secondarySources = proc (conf: TestConf, sources: auto) =
secondarySources = proc (conf: TestConf, sources: auto)
{.gcsafe, raises: [ConfigurationError].} =
sources.addConfigFile(Envvar, InputFile(prefix))
)
ok(conf)
Expand All @@ -49,7 +48,6 @@ proc load*(T: type TestConf, prefix: string): ConfResult[T] =

{.pop.}


suite "nim-confutils - envvar":
test "load configuration from environment variables":
## Given
Expand Down Expand Up @@ -78,4 +76,4 @@ suite "nim-confutils - envvar":
conf.configFile.isSome()
conf.configFile.get().string == configFile

conf.testFile.isNone()
conf.testFile.isNone()
12 changes: 5 additions & 7 deletions waku/common/confutils/envvar/std/net.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ when (NimMajor, NimMinor) < (1, 4):
else:
{.push raises: [].}


import
std/strutils,
stew/shims/net
Expand All @@ -14,15 +13,14 @@ export
net,
envvar_serialization


proc readValue*(r: var EnvvarReader, value: var ValidIpAddress) {.raises: [SerializationError].} =
try:
value = ValidIpAddress.init(r.readValue(string))
except ValueError:
raise newException(EnvvarError, "Invalid IP address")
except ValueError, IOError:
raise newException(SerializationError, "Invalid IP address: " & getCurrentExceptionMsg())

proc readValue*(r: var EnvvarReader, value: var Port) {.raises: [SerializationError, ValueError].} =
proc readValue*(r: var EnvvarReader, value: var Port) {.raises: [SerializationError].} =
try:
value = parseUInt(r.readValue(string)).Port
except ValueError:
raise newException(EnvvarError, "Invalid Port")
except ValueError, IOError:
raise newException(SerializationError, "Invalid Port: " & getCurrentExceptionMsg())
14 changes: 11 additions & 3 deletions waku/common/envvar_serialization/reader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ proc handleReadException*(r: EnvvarReader,
proc init*(T: type EnvvarReader, prefix: string): T =
result.prefix = prefix

proc readValue*[T](r: var EnvvarReader, value: var T) {.raises: [ValueError, SerializationError].} =
proc readValue*[T](r: var EnvvarReader, value: var T) {.raises: [SerializationError].} =
mixin readValue

when T is string:
Expand All @@ -48,7 +48,11 @@ proc readValue*[T](r: var EnvvarReader, value: var T) {.raises: [ValueError, Ser

elif T is (SomePrimitives or range):
let key = constructKey(r.prefix, r.key)
getValue(key, value)
try:
getValue(key, value)
except ValueError:
raise newException(SerializationError,
"Couldn't getValue SomePrimitives: " & getCurrentExceptionMsg())

elif T is Option:
template getUnderlyingType[T](_: Option[T]): untyped = T
Expand All @@ -58,7 +62,11 @@ proc readValue*[T](r: var EnvvarReader, value: var T) {.raises: [ValueError, Ser
when uType is string:
value = some(os.getEnv(key))
else:
value = some(r.readValue(uType))
try:
value = some(r.readValue(uType))
except ValueError, IOError:
raise newException(SerializationError,
"Couldn't read Option value: " & getCurrentExceptionMsg())

elif T is (seq or array):
when uTypeIsPrimitives(T):
Expand Down

0 comments on commit c2e905e

Please sign in to comment.