Merge pull request #69 from status-im/error-handling
Better error handling
This commit is contained in:
commit
b6336cb725
|
@ -12,7 +12,7 @@ requires "nim >= 0.17.3",
|
||||||
"chronos",
|
"chronos",
|
||||||
"httputils",
|
"httputils",
|
||||||
"chronicles",
|
"chronicles",
|
||||||
"news",
|
"news >= 0.2 & < 0.3",
|
||||||
"chronicles",
|
"chronicles",
|
||||||
"json_serialization"
|
"json_serialization"
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ type
|
||||||
awaiting*: Table[ClientId, Future[Response]]
|
awaiting*: Table[ClientId, Future[Response]]
|
||||||
nextId: ClientId
|
nextId: ClientId
|
||||||
methodHandlers: Table[string, proc(j: JsonNode) {.gcsafe.}]
|
methodHandlers: Table[string, proc(j: JsonNode) {.gcsafe.}]
|
||||||
|
onDisconnect*: proc() {.gcsafe.}
|
||||||
|
|
||||||
Response* = tuple[error: bool, result: JsonNode]
|
Response* = tuple[error: bool, result: JsonNode]
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import ../client, chronos, tables, json, strtabs, chronicles
|
import ../client, chronos, tables, json, strtabs
|
||||||
|
|
||||||
const newsUseChronos = true
|
const newsUseChronos = true
|
||||||
include news
|
include news
|
||||||
|
@ -33,17 +33,29 @@ method call*(self: RpcWebSocketClient, name: string,
|
||||||
result = await newFut
|
result = await newFut
|
||||||
|
|
||||||
proc processData(client: RpcWebSocketClient) {.async.} =
|
proc processData(client: RpcWebSocketClient) {.async.} =
|
||||||
while true:
|
var error: ref Exception
|
||||||
|
try:
|
||||||
while true:
|
while true:
|
||||||
var value = await client.transport.receivePacket()
|
var value = await client.transport.receivePacket()
|
||||||
if value == "":
|
if value == "":
|
||||||
# transmission ends
|
# transmission ends
|
||||||
client.transport.close()
|
|
||||||
break
|
break
|
||||||
|
|
||||||
client.processMessage(value)
|
client.processMessage(value)
|
||||||
# async loop reconnection and waiting
|
except CatchableError as e:
|
||||||
client.transport = await newWebSocket(client.uri)
|
error = e
|
||||||
|
|
||||||
|
client.transport.close()
|
||||||
|
client.transport = nil
|
||||||
|
|
||||||
|
if client.awaiting.len != 0:
|
||||||
|
if error.isNil:
|
||||||
|
error = newException(IOError, "Transport was closed while waiting for response")
|
||||||
|
for k, v in client.awaiting:
|
||||||
|
v.fail(error)
|
||||||
|
client.awaiting.clear()
|
||||||
|
if not client.onDisconnect.isNil:
|
||||||
|
client.onDisconnect()
|
||||||
|
|
||||||
proc connect*(client: RpcWebSocketClient, uri: string, headers: StringTableRef = nil) {.async.} =
|
proc connect*(client: RpcWebSocketClient, uri: string, headers: StringTableRef = nil) {.async.} =
|
||||||
var headers = headers
|
var headers = headers
|
||||||
|
@ -56,11 +68,7 @@ proc connect*(client: RpcWebSocketClient, uri: string, headers: StringTableRef =
|
||||||
client.transport = await newWebSocket(uri, headers)
|
client.transport = await newWebSocket(uri, headers)
|
||||||
client.uri = uri
|
client.uri = uri
|
||||||
client.loop = processData(client)
|
client.loop = processData(client)
|
||||||
client.loop.addCallback do(data: pointer):
|
|
||||||
if client.loop.failed:
|
|
||||||
let err = client.loop.readError()
|
|
||||||
error "websocket rpc", msg = err.msg, stacktrace = err.getStackTrace()
|
|
||||||
|
|
||||||
method close*(client: RpcWebSocketClient) {.async.} =
|
method close*(client: RpcWebSocketClient) {.async.} =
|
||||||
# TODO: Stop the processData loop
|
if not client.transport.isNil:
|
||||||
client.transport.close()
|
client.loop.cancel()
|
||||||
|
|
|
@ -41,7 +41,7 @@ proc addEthRpcs*(server: RpcServer) =
|
||||||
## Returns the SHA3 result of the given string.
|
## Returns the SHA3 result of the given string.
|
||||||
# TODO: Capture error on malformed input
|
# TODO: Capture error on malformed input
|
||||||
var rawData: seq[byte]
|
var rawData: seq[byte]
|
||||||
rawData = data.string.fromHex
|
rawData = nimcrypto.fromHex(data.string)
|
||||||
# data will have 0x prefix
|
# data will have 0x prefix
|
||||||
result = hexDataStr "0x" & $keccak_256.digest(rawData)
|
result = hexDataStr "0x" & $keccak_256.digest(rawData)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue