mirror of
https://github.com/codex-storage/nim-json-rpc.git
synced 2025-02-24 17:08:17 +00:00
39 lines
1.2 KiB
Nim
39 lines
1.2 KiB
Nim
import rpcconsts, options, json
|
|
|
|
type
|
|
RpcJsonError* = enum rjeInvalidJson, rjeVersionError, rjeNoMethod, rjeNoId
|
|
RpcJsonErrorContainer* = tuple[err: RpcJsonError, msg: string]
|
|
|
|
const
|
|
jsonErrorMessages*: array[RpcJsonError, (int, string)] =
|
|
[
|
|
(JSON_PARSE_ERROR, "Invalid JSON"),
|
|
(INVALID_REQUEST, "JSON 2.0 required"),
|
|
(INVALID_REQUEST, "No method requested"),
|
|
(INVALID_REQUEST, "No id specified")
|
|
]
|
|
|
|
template jsonValid*(jsonString: string, node: var JsonNode): (bool, string) =
|
|
var
|
|
valid = true
|
|
msg = ""
|
|
try: node = parseJson(line)
|
|
except:
|
|
valid = false
|
|
msg = getCurrentExceptionMsg()
|
|
(valid, msg)
|
|
|
|
proc checkJsonErrors*(line: string, node: var JsonNode): Option[RpcJsonErrorContainer] =
|
|
## Tries parsing line into node, if successful checks required fields
|
|
## Returns: error state or none
|
|
let res = jsonValid(line, node)
|
|
if not res[0]:
|
|
return some((rjeInvalidJson, res[1]))
|
|
if not node.hasKey("jsonrpc"):
|
|
return some((rjeVersionError, ""))
|
|
if not node.hasKey("method"):
|
|
return some((rjeNoMethod, ""))
|
|
if not node.hasKey("id"):
|
|
return some((rjeNoId, ""))
|
|
return none(RpcJsonErrorContainer)
|