Skip to content

Commit

Permalink
improve RPC client logging (#171)
Browse files Browse the repository at this point in the history
We currently only debug log successfully sent messages to RPC.
For debugging, it would be better to see the attempt and any failures.
Adjust logging to provide more information when debugging.
Also include the message name at debug level instead of just trace.
  • Loading branch information
etan-status authored Aug 23, 2023
1 parent b067143 commit 60c4c9b
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions json_rpc/clients/httpclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ method call*(client: RpcHttpClient, name: string,
except CatchableError as exc: # shouldn't happen
debug "Error closing JSON-RPC HTTP resuest/response", err = exc.msg

debug "Sending message to RPC server",
address = client.httpAddress, msg_len = len(reqBody), name
trace "Message", msg = reqBody

req = HttpClientRequestRef.post(client.httpSession,
client.httpAddress.get,
body = reqBody.toOpenArrayByte(0, reqBody.len - 1),
Expand All @@ -84,27 +88,29 @@ method call*(client: RpcHttpClient, name: string,
try:
await req.send()
except CancelledError as e:
debug "Cancelled POST Request with JSON-RPC", e = e.msg
closeRefs()
raise e
except CatchableError as e:
debug "Failed to send POST Request with JSON-RPC", e = e.msg
closeRefs()
raise (ref RpcPostError)(msg: "Failed to send POST Request with JSON-RPC: " & e.msg, parent: e)

if res.status < 200 or res.status >= 300: # res.status is not 2xx (success)
debug "Unsuccessful POST Request with JSON-RPC",
status = res.status, reason = res.reason
closeRefs()
raise (ref ErrorResponse)(status: res.status, msg: res.reason)

debug "Message sent to RPC server",
address = client.httpAddress, msg_len = len(reqBody)
trace "Message", msg = reqBody

let resBytes =
try:
await res.getBodyBytes(client.maxBodySize)
except CancelledError as e:
debug "Cancelled POST Response for JSON-RPC", e = e.msg
closeRefs()
raise e
except CatchableError as e:
debug "Failed to read POST Response for JSON-RPC", e = e.msg
closeRefs()
raise (ref FailedHttpResponse)(msg: "Failed to read POST Response for JSON-RPC: " & e.msg, parent: e)

Expand All @@ -122,6 +128,7 @@ method call*(client: RpcHttpClient, name: string,
client.processMessage(resText)
except CatchableError as e:
# Need to clean up in case the answer was invalid
debug "Failed to process POST Response for JSON-RPC", e = e.msg
client.awaiting.del(id)
closeRefs()
raise e
Expand All @@ -136,6 +143,7 @@ method call*(client: RpcHttpClient, name: string,
return newFut.read()
else:
# TODO: Provide more clarity regarding the failure here
debug "Invalid POST Response for JSON-RPC"
raise newException(InvalidResponse, "Invalid response")

proc connect*(client: RpcHttpClient, url: string) {.async.} =
Expand Down

0 comments on commit 60c4c9b

Please sign in to comment.