replace getCurrentExceptionMsg() and delete some unused imports

This commit is contained in:
Ștefan Talpalaru 2019-12-02 12:14:00 +01:00
parent b6336cb725
commit 72a99cc977
No known key found for this signature in database
GPG Key ID: CBF7934204F1B6F9
10 changed files with 40 additions and 42 deletions

2
.gitignore vendored
View File

@ -11,8 +11,8 @@
*.exe
*.dll
# Ignore the nimcache folders
nimcache/
/build
# Ignore editor settings
.vscode

View File

@ -16,14 +16,11 @@ requires "nim >= 0.17.3",
"chronicles",
"json_serialization"
proc configForTests() =
--hints: off
--debuginfo
--path: "."
--run
--forceBuild
--threads: on
proc buildBinary(name: string, srcDir = "./", params = "", cmdParams = "", lang = "c") =
if not dirExists "build":
mkDir "build"
exec "nim " & lang & " --out:./build/" & name & " " & params & " " & srcDir & name & ".nim" & " " & cmdParams
task test, "run tests":
configForTests()
setCommand "c", "tests/all.nim"
buildBinary "all", "tests/", "-r -f --hints:off --debuginfo --path:'.' --threads:on -d:chronicles_log_level=ERROR"

View File

@ -107,9 +107,9 @@ proc recvData(transp: StreamTransport): Future[string] {.async.} =
# remote peer disconnected
debug "Remote peer disconnected", address = transp.remoteAddress()
error = true
except TransportOsError:
except TransportOsError as exc:
debug "Problems with networking", address = transp.remoteAddress(),
error = getCurrentExceptionMsg()
error = exc.msg
error = true
if error or not transp.validateResponse(header):
@ -146,9 +146,9 @@ proc recvData(transp: StreamTransport): Future[string] {.async.} =
# remote peer disconnected
debug "Remote peer disconnected", address = transp.remoteAddress()
error = true
except TransportOsError:
except TransportOsError as exc:
debug "Problems with networking", address = transp.remoteAddress(),
error = getCurrentExceptionMsg()
error = exc.msg
error = true
if error:

View File

@ -69,10 +69,11 @@ template jsonValid*(jsonString: string, node: var JsonNode): (bool, string) =
var
valid = true
msg = ""
try: node = parseJson(line)
except:
try:
node = parseJson(line)
except CatchableError as exc:
valid = false
msg = getCurrentExceptionMsg()
msg = exc.msg
debug "Cannot process json", json = jsonString, msg = msg
(valid, msg)
@ -190,8 +191,8 @@ proc hasReturnType(params: NimNode): bool =
template trap(path: string, body: untyped): untyped =
try:
body
except:
let msg = getCurrentExceptionMsg()
except CatchableError as exc:
let msg = exc.msg
debug "Error occurred within RPC ", path = path, errorMessage = msg
result = %*{codeField: %SERVER_ERROR, messageField: %msg}

View File

@ -1,4 +1,4 @@
import json, tables, options, macros
import json, macros
import chronos, router, chronicles
import jsonmarshal

View File

@ -129,14 +129,14 @@ proc processClient(server: StreamServer,
debug "Remote peer disconnected", address = transp.remoteAddress()
await transp.closeWait()
break
except TransportOsError:
except TransportOsError as exc:
debug "Problems with networking", address = transp.remoteAddress(),
error = getCurrentExceptionMsg()
error = exc.msg
await transp.closeWait()
break
except:
except CatchableError as exc:
debug "Unknown exception", address = transp.remoteAddress(),
error = getCurrentExceptionMsg()
error = exc.msg
await transp.closeWait()
break
@ -168,9 +168,9 @@ proc processClient(server: StreamServer,
debug "Remote peer disconnected", address = transp.remoteAddress()
await transp.closeWait()
break
except TransportOsError:
except TransportOsError as exc:
debug "Problems with networking", address = transp.remoteAddress(),
error = getCurrentExceptionMsg()
error = exc.msg
await transp.closeWait()
break
@ -216,9 +216,9 @@ proc addStreamServer*(server: RpcHttpServer, address: TransportAddress) =
var transServer = createStreamServer(address, processClient,
{ReuseAddr}, udata = server)
server.servers.add(transServer)
except:
except CatchableError as exc:
error "Failed to create server", address = $address,
message = getCurrentExceptionMsg()
message = exc.msg
if len(server.servers) == 0:
# Server was not bound, critical error.

View File

@ -35,8 +35,8 @@ proc addStreamServer*(server: RpcSocketServer, address: TransportAddress) =
info "Creating server on ", address = $address
var transportServer = createStreamServer(address, processClient, {ReuseAddr}, udata = server)
server.servers.add(transportServer)
except:
error "Failed to create server", address = $address, message = getCurrentExceptionMsg()
except CatchableError as exc:
error "Failed to create server", address = $address, message = exc.msg
if len(server.servers) == 0:
# Server was not bound, critical error.

View File

@ -17,7 +17,7 @@ server.rpc("rpc") do(a: int, b: int):
server.rpc("makeError"):
if true:
raise newException(ValueError, "Test")
raise newException(ValueError, "Test")
proc testMissingRpc: Future[Response] {.async.} =
var fut = client.call("phantomRpc", %[])
@ -51,23 +51,23 @@ suite "RPC Errors":
check res.error == true and
res.result["message"] == %"Method not found" and
res.result["data"] == %"phantomRpc is not a registered method."
except:
echo "Error ", getCurrentExceptionMsg()
except CatchableError as exc:
echo "Error ", exc.msg
#[test "Incorrect json version":
#expect ValueError:
try:
let res = waitFor testInvalidJsonVer()
check res.error == true and res.result["message"] == %"JSON 2.0 required"
except:
echo "Error ", getCurrentExceptionMsg()
except CatchableError as exc:
echo "Error ", exc.msg
]#
test "Raising exceptions":
#expect ValueError:
try:
let res = waitFor testRaise()
except:
echo "Error ", getCurrentExceptionMsg()
except CatchableError as exc:
echo "Error ", exc.msg
test "Malformed json":
# TODO: We time out here because the server won't be able to
@ -75,6 +75,6 @@ suite "RPC Errors":
try:
let res = waitFor testMalformed()
check res.error == true and res.result == %"Timeout"
except:
echo "Error ", getCurrentExceptionMsg()
except CatchableError as exc:
echo "Error ", exc.msg

View File

@ -1,6 +1,6 @@
import unittest, json, tables
import ../json_rpc/[rpcclient, rpcserver]
import stint, ethtypes, ethprocs, stintjson, nimcrypto, ethhexstrings, chronicles
import stint, ethtypes, ethprocs, stintjson, chronicles
from os import getCurrentDir, DirSep
from strutils import rsplit

View File

@ -1,4 +1,4 @@
import unittest, json, tables, chronicles, options
import unittest, json, chronicles, options
import ../json_rpc/rpcserver
type