Skip to content

Commit 3cebb14

Browse files
committed
return server exception message on api exception
This change is to return the exception encountered while executing server function back to the client. The exception is returned in string form in the `data` field of the error response. To customize messages at server side: - catch exceptions and rethrow as custom exception - override string conversion functions of exceptions
1 parent e71c9e1 commit 3cebb14

4 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/APIResponder.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function call_api(api::APISpec, conn::APIResponder, args, data::Dict{Symbol,Any}
9898
respond(conn, Nullable(api), :success, result)
9999
catch ex
100100
logerr("api_exception: ", ex)
101-
respond(conn, Nullable(api), :api_exception)
101+
respond(conn, Nullable(api), :api_exception, string(ex))
102102
end
103103
end
104104

test/clnt.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,23 @@ function run_clnt(fmt, tport)
6161
println("time for $NCALLS calls to testbinary: $t secs @ $(t/NCALLS) per call")
6262

6363
# Test Array invocation
64+
println("testing array invocation...")
6465
resp = apicall(apiclnt, "testArray", Float64[1.0 2.0; 3.0 4.0])
6566
@test fnresponse(apiclnt.format, resp) == 12
6667

6768
# Test unknown function call
69+
println("testing unknown method handling...")
6870
resp = apicall(apiclnt, "testNoSuchMethod", Float64[1.0 2.0; 3.0 4.0])
6971
@test resp["code"] == 404
7072
resp = apicall(apiclnt, "testArray", "no such argument")
7173
@test resp["code"] == 500
74+
@test contains(resp["data"], "MethodError")
75+
76+
# Test exceptions
77+
println("testing server method exception handling...")
78+
resp = apicall(apiclnt, "testException")
79+
@test resp["code"] == 500
80+
@test contains(resp["data"]["data"], "testing exception handling")
7281

7382
close(ctx)
7483
close(tport)

test/srvr.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function run_srvr(fmt, tport, async=false, open=false)
2525
register(api, testbinary; resp_headers=BINARY_RESP_HDRS)
2626
register(api, testArray)
2727
register(api, testFile; resp_json=true, resp_headers=JSON_RESP_HDRS)
28+
register(api, testException; resp_json=true, resp_headers=JSON_RESP_HDRS)
2829

2930
process(api; async=async)
3031
end

test/srvrfn.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ function testFile(;filename=nothing, filedata=nothing)
2121
#println("[", String(filedata), "]")
2222
string(length(filename)) * "," * string(length(filedata))
2323
end
24+
25+
testException() = error("testing exception handling")

0 commit comments

Comments
 (0)