mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-04-10 13:53:11 +00:00
chore: Add pre-check of options used in config Json for liblogosdelivery pre-createNode, treat unrecognized options as error (#3801)
* Add pre-check of options used in config Json for logos-delivery-api pre-createNode, treat unrecognized options as error * Collect all unrecognized options and report them at once. * Refactor json config parsing and error detection
This commit is contained in:
parent
59bd365c16
commit
5503529531
@ -1,4 +1,4 @@
|
||||
import std/[json, strutils]
|
||||
import std/[json, strutils, tables]
|
||||
import chronos, chronicles, results, confutils, confutils/std/net, ffi
|
||||
import
|
||||
waku/factory/waku,
|
||||
@ -23,22 +23,50 @@ registerReqFFI(CreateNodeRequest, ctx: ptr FFIContext[Waku]):
|
||||
try:
|
||||
jsonNode = parseJson($configJson)
|
||||
except Exception:
|
||||
let exceptionMsg = getCurrentExceptionMsg()
|
||||
error "Failed to parse config JSON",
|
||||
error = exceptionMsg, configJson = $configJson
|
||||
return err(
|
||||
"Failed to parse config JSON: " & getCurrentExceptionMsg() &
|
||||
" configJson string: " & $configJson
|
||||
"Failed to parse config JSON: " & exceptionMsg & " configJson string: " &
|
||||
$configJson
|
||||
)
|
||||
|
||||
var jsonFields: Table[string, (string, JsonNode)]
|
||||
for key, value in jsonNode:
|
||||
let lowerKey = key.toLowerAscii()
|
||||
|
||||
if jsonFields.hasKey(lowerKey):
|
||||
error "Duplicate configuration option found when normalized to lowercase",
|
||||
key = key
|
||||
return err(
|
||||
"Duplicate configuration option found when normalized to lowercase: '" & key &
|
||||
"'"
|
||||
)
|
||||
|
||||
jsonFields[lowerKey] = (key, value)
|
||||
|
||||
for confField, confValue in fieldPairs(conf):
|
||||
if jsonNode.contains(confField):
|
||||
let formattedString = ($jsonNode[confField]).strip(chars = {'\"'})
|
||||
let lowerField = confField.toLowerAscii()
|
||||
if jsonFields.hasKey(lowerField):
|
||||
let (jsonKey, jsonValue) = jsonFields[lowerField]
|
||||
let formattedString = ($jsonValue).strip(chars = {'\"'})
|
||||
try:
|
||||
confValue = parseCmdArg(typeof(confValue), formattedString)
|
||||
except Exception:
|
||||
return err(
|
||||
"Failed to parse field '" & confField & "': " & getCurrentExceptionMsg() &
|
||||
". Value: " & formattedString
|
||||
"Failed to parse field '" & confField & "' from JSON key '" & jsonKey & "': " &
|
||||
getCurrentExceptionMsg() & ". Value: " & formattedString
|
||||
)
|
||||
|
||||
jsonFields.del(lowerField)
|
||||
|
||||
if jsonFields.len > 0:
|
||||
var unknownKeys = newSeq[string]()
|
||||
for _, (jsonKey, _) in pairs(jsonFields):
|
||||
unknownKeys.add(jsonKey)
|
||||
error "Unrecognized configuration option(s) found", option = unknownKeys
|
||||
return err("Unrecognized configuration option(s) found: " & $unknownKeys)
|
||||
|
||||
# Create the node
|
||||
ctx.myLib[] = (await api.createNode(conf)).valueOr:
|
||||
let errMsg = $error
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user