Skip to content

Commit

Permalink
cmd: better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ziogaschr committed Nov 27, 2020
1 parent 5a4d412 commit 4f7e7cc
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1733,27 +1733,34 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {

if ctx.GlobalIsSet(EthProtocolsFlag.Name) {
protocolVersions := SplitAndTrim(ctx.GlobalString(EthProtocolsFlag.Name))
if len(protocolVersions) > 0 {
for _, versionString := range protocolVersions {
version, err := strconv.ParseUint(versionString, 10, 0)
if err != nil {
continue
}
if len(protocolVersions) == 0 {
Fatalf("--%s must be comma separated list of %s", EthProtocolsFlag.Name, strings.Join(strings.Fields(fmt.Sprint(eth.DefaultProtocolVersions)), ","))
}

isValid := false
for _, proto := range eth.DefaultProtocolVersions {
if proto == uint(version) {
isValid = true
}
}
seenVersions := map[uint]interface{}{}
for _, versionString := range protocolVersions {
version, err := strconv.ParseUint(versionString, 10, 0)
if err != nil {
Fatalf("--%s has invalid value \"%v\" with error: %v", EthProtocolsFlag.Name, versionString, err)
}

if !isValid {
Fatalf("--%s must be comma separated list of %s", EthProtocolsFlag.Name, strings.Join(strings.Fields(fmt.Sprint(eth.DefaultProtocolVersions)), ","))
if _, duplicate := seenVersions[uint(version)]; duplicate {
Fatalf("--%s has duplicate version of %v", EthProtocolsFlag.Name, versionString)
}

isValid := false
for _, proto := range eth.DefaultProtocolVersions {
if proto == uint(version) {
isValid = true
seenVersions[uint(version)] = nil
break
}
cfg.ProtocolVersions = append(cfg.ProtocolVersions, uint(version))
}
} else {
Fatalf("--%s must be comma separated list of %s", EthProtocolsFlag.Name, strings.Join(strings.Fields(fmt.Sprint(eth.DefaultProtocolVersions)), ","))

if !isValid {
Fatalf("--%s must be comma separated list of %s", EthProtocolsFlag.Name, strings.Join(strings.Fields(fmt.Sprint(eth.DefaultProtocolVersions)), ","))
}
cfg.ProtocolVersions = append(cfg.ProtocolVersions, uint(version))
}
}

Expand Down

0 comments on commit 4f7e7cc

Please sign in to comment.