Skip to content

Commit

Permalink
fail on error while processing secondarySources (#82)
Browse files Browse the repository at this point in the history
The way how `secondarySources` are used in `status-im/nimbus-eth2`,
they call `addConfigFile` again which may raise `ConfigurationError`.
When that happened, loading the primary config file didn't result in
`fail`, which differs in error handling from errors within itself
as opposed to within `secondarySources`. To keep callers concise,
apply same error handling behaviour regardless of whether the error
occurred during primary or secondary sources processing.
  • Loading branch information
etan-status authored Aug 17, 2023
1 parent dbe8d61 commit 3c7c1cf
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions confutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ macro configurationRtti(RecordType: type): untyped =

proc addConfigFile*(secondarySources: auto,
Format: type,
path: InputFile) =
path: InputFile) {.raises: [ConfigurationError].} =
try:
secondarySources.data.add loadFile(Format, string path,
type(secondarySources.data[0]))
Expand Down Expand Up @@ -892,9 +892,11 @@ proc loadImpl[C, SecondarySources](
printUsage = true,
quitOnFailure = true,
secondarySourcesRef: ref SecondarySources,
secondarySources: proc (config: Configuration,
sources: ref SecondarySources) {.gcsafe.} = nil,
envVarsPrefix = getAppFilename()): Configuration =
secondarySources: proc (
config: Configuration, sources: ref SecondarySources
) {.gcsafe, raises: [ConfigurationError].} = nil,
envVarsPrefix = getAppFilename()
): Configuration {.raises: [ConfigurationError].} =
## Loads a program configuration by parsing command-line arguments
## and a standard set of config files that can specify:
##
Expand Down Expand Up @@ -1135,7 +1137,10 @@ proc loadImpl[C, SecondarySources](
activateCmd(subCmdDiscriminator, defaultCmd)

if secondarySources != nil:
secondarySources(result, secondarySourcesRef)
try:
secondarySources(result, secondarySourcesRef)
except ConfigurationError as err:
fail "Failed to load secondary sources: '$1'" % [err.msg]

proc processMissingOpts(
conf: var Configuration, cmd: CmdInfo) {.raises: [ConfigurationError].} =
Expand Down

0 comments on commit 3c7c1cf

Please sign in to comment.