mirror of
https://github.com/logos-storage/nim-json-rpc.git
synced 2026-05-28 13:29:29 +00:00
Simplify error trapping
This commit is contained in:
parent
2ed141b0de
commit
49085bbd28
@ -144,7 +144,7 @@ proc route*(router: RpcRouter, data: string): Future[string] {.async, gcsafe.} =
|
|||||||
else:
|
else:
|
||||||
node["id"]
|
node["id"]
|
||||||
let
|
let
|
||||||
# fixed error code and message
|
# const error code and message
|
||||||
errKind = jsonErrorMessages[errState.err]
|
errKind = jsonErrorMessages[errState.err]
|
||||||
# pass on the actual error message
|
# pass on the actual error message
|
||||||
fullMsg = errKind[1] & " " & errState[1]
|
fullMsg = errKind[1] & " " & errState[1]
|
||||||
@ -181,6 +181,14 @@ proc hasReturnType(params: NimNode): bool =
|
|||||||
params[0].kind != nnkEmpty:
|
params[0].kind != nnkEmpty:
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
|
template trap(path: string, body: untyped): untyped =
|
||||||
|
try:
|
||||||
|
body
|
||||||
|
except:
|
||||||
|
let msg = getCurrentExceptionMsg()
|
||||||
|
debug "Error occurred within RPC ", path = path, errorMessage = msg
|
||||||
|
result = %*{codeField: %SERVER_ERROR, messageField: %msg}
|
||||||
|
|
||||||
macro rpc*(server: RpcRouter, path: string, body: untyped): untyped =
|
macro rpc*(server: RpcRouter, path: string, body: untyped): untyped =
|
||||||
## Define a remote procedure call.
|
## Define a remote procedure call.
|
||||||
## Input and return parameters are defined using the ``do`` notation.
|
## Input and return parameters are defined using the ``do`` notation.
|
||||||
@ -210,47 +218,37 @@ macro rpc*(server: RpcRouter, path: string, body: untyped): untyped =
|
|||||||
var
|
var
|
||||||
setup = jsonToNim(parameters, paramsIdent)
|
setup = jsonToNim(parameters, paramsIdent)
|
||||||
procBody = if body.kind == nnkStmtList: body else: body.body
|
procBody = if body.kind == nnkStmtList: body else: body.body
|
||||||
errTrappedBody = quote do:
|
|
||||||
try:
|
|
||||||
`procBody`
|
|
||||||
except:
|
|
||||||
let msg = getCurrentExceptionMsg()
|
|
||||||
debug "Error occurred within RPC ", path = `path`, errorMessage = msg
|
|
||||||
`errJson` = %*{codeField: %SERVER_ERROR, messageField: %msg}
|
|
||||||
|
|
||||||
if parameters.hasReturnType:
|
if parameters.hasReturnType:
|
||||||
let returnType = parameters[0]
|
let returnType = parameters[0]
|
||||||
|
|
||||||
# delegate async proc allows return and setting of result as native type
|
# delegate async proc allows return and setting of result as native type
|
||||||
result.add(quote do:
|
result.add(quote do:
|
||||||
proc `doMain`(`paramsIdent`: JsonNode, `errJson`: var JsonNode): `returnType` =
|
proc `doMain`(`paramsIdent`: JsonNode): `returnType` =
|
||||||
`setup`
|
`setup`
|
||||||
`errTrappedBody`
|
`procBody`
|
||||||
)
|
)
|
||||||
|
|
||||||
if returnType == ident"JsonNode":
|
if returnType == ident"JsonNode":
|
||||||
# `JsonNode` results don't need conversion
|
# `JsonNode` results don't need conversion
|
||||||
result.add( quote do:
|
result.add( quote do:
|
||||||
proc `procName`(`paramsIdent`: JsonNode): Future[JsonNode] {.async.} =
|
proc `procName`(`paramsIdent`: JsonNode): Future[JsonNode] {.async.} =
|
||||||
var `errJson`: JsonNode
|
trap(`pathStr`):
|
||||||
`res` = `doMain`(`paramsIdent`, `errJson`)
|
`res` = `doMain`(`paramsIdent`)
|
||||||
if `errJson` != nil: `res` = `errJson`
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
result.add(quote do:
|
result.add(quote do:
|
||||||
proc `procName`(`paramsIdent`: JsonNode): Future[JsonNode] {.async.} =
|
proc `procName`(`paramsIdent`: JsonNode): Future[JsonNode] {.async.} =
|
||||||
var `errJson`: JsonNode
|
trap(`pathStr`):
|
||||||
`res` = %`doMain`(`paramsIdent`, `errJson`)
|
`res` = %`doMain`(`paramsIdent`)
|
||||||
if `errJson` != nil: `res` = `errJson`
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
# no return types, inline contents
|
# no return types, inline contents
|
||||||
result.add(quote do:
|
result.add(quote do:
|
||||||
proc `procName`(`paramsIdent`: JsonNode): Future[JsonNode] {.async.} =
|
proc `procName`(`paramsIdent`: JsonNode): Future[JsonNode] {.async.} =
|
||||||
`setup`
|
`setup`
|
||||||
var `errJson`: JsonNode
|
trap(`pathStr`):
|
||||||
`errTrappedBody`
|
`procBody`
|
||||||
if `errJson` != nil: `res` = `errJson`
|
|
||||||
)
|
)
|
||||||
result.add( quote do:
|
result.add( quote do:
|
||||||
`server`.register(`path`, `procName`)
|
`server`.register(`path`, `procName`)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user