Skip to content

Commit

Permalink
return server exception message on api exception
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tanmaykm committed Sep 16, 2017
1 parent e71c9e1 commit 3cebb14
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/APIResponder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function call_api(api::APISpec, conn::APIResponder, args, data::Dict{Symbol,Any}
respond(conn, Nullable(api), :success, result)
catch ex
logerr("api_exception: ", ex)
respond(conn, Nullable(api), :api_exception)
respond(conn, Nullable(api), :api_exception, string(ex))
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/clnt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,23 @@ function run_clnt(fmt, tport)
println("time for $NCALLS calls to testbinary: $t secs @ $(t/NCALLS) per call")

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

# Test unknown function call
println("testing unknown method handling...")
resp = apicall(apiclnt, "testNoSuchMethod", Float64[1.0 2.0; 3.0 4.0])
@test resp["code"] == 404
resp = apicall(apiclnt, "testArray", "no such argument")
@test resp["code"] == 500
@test contains(resp["data"], "MethodError")

# Test exceptions
println("testing server method exception handling...")
resp = apicall(apiclnt, "testException")
@test resp["code"] == 500
@test contains(resp["data"]["data"], "testing exception handling")

close(ctx)
close(tport)
Expand Down
1 change: 1 addition & 0 deletions test/srvr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function run_srvr(fmt, tport, async=false, open=false)
register(api, testbinary; resp_headers=BINARY_RESP_HDRS)
register(api, testArray)
register(api, testFile; resp_json=true, resp_headers=JSON_RESP_HDRS)
register(api, testException; resp_json=true, resp_headers=JSON_RESP_HDRS)

process(api; async=async)
end
Expand Down
2 changes: 2 additions & 0 deletions test/srvrfn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ function testFile(;filename=nothing, filedata=nothing)
#println("[", String(filedata), "]")
string(length(filename)) * "," * string(length(filedata))
end

testException() = error("testing exception handling")

0 comments on commit 3cebb14

Please sign in to comment.