Skip to content

Commit 2079986

Browse files
authored
Merge pull request #70 from Seelengrab/showerror_fix
Allow other applications to hook into `showerror`
2 parents c5f2443 + 962d276 commit 2079986

File tree

2 files changed

+90
-30
lines changed

2 files changed

+90
-30
lines changed

src/core.jl

Lines changed: 89 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,99 @@
1+
"""
2+
JSONRPCError
3+
4+
An object representing a JSON-RPC Error.
5+
6+
Fields:
7+
* code::Int
8+
* msg::AbstractString
9+
* data::Any
10+
11+
See Section 5.1 of the JSON RPC 2.0 specification for more information.
12+
"""
113
struct JSONRPCError <: Exception
214
code::Int
315
msg::AbstractString
416
data::Any
517
end
618

19+
"""
20+
SERVER_ERROR_END
21+
22+
The end of the range of server-reserved errors.
23+
24+
These are JSON-RPC server errors that are free for the taking
25+
for JSON-RPC server implementations. Applications making use of
26+
this library should NOT define new errors in this range.
27+
"""
28+
const SERVER_ERROR_END = -32000
29+
30+
"""
31+
SERVER_ERROR_START
32+
33+
The start of the range of server-reserved errors.
34+
35+
These are JSON-RPC server errors that are free for the taking
36+
for JSON-RPC server implementations. Applications making use of
37+
this library should NOT define new errors in this range.
38+
"""
39+
const SERVER_ERROR_START = -32099
40+
41+
"""
42+
PARSE_ERROR
43+
44+
Invalid JSON was received by the server.
45+
An error occurred on the server while parsing the JSON text.
46+
"""
47+
const PARSE_ERROR = -32700
48+
49+
"""
50+
INVALID_REQUEST
51+
52+
The JSON sent is not a valid Request object.
53+
"""
54+
const INVALID_REQUEST = -32600
55+
56+
"""
57+
METHOD_NOT_FOUND
58+
59+
The method does not exist / is not available.
60+
"""
61+
const METHOD_NOT_FOUND = -32601
62+
63+
"""
64+
INVALID_PARAMS
65+
66+
Invalid method parameter(s).
67+
"""
68+
const INVALID_PARAMS = -32602
69+
70+
"""
71+
INTERNAL_ERROR
72+
73+
Internal JSON-RPC error.
74+
"""
75+
const INTERNAL_ERROR = -32603
76+
77+
"""
78+
RPCErrorStrings
79+
80+
A `Base.IdDict` containing the mapping of JSON-RPC error codes to a short, descriptive string.
81+
82+
Use this to hook into `showerror(io::IO, ::JSONRPCError)` for display purposes. A default fallback to `"Unknown"` exists.
83+
"""
84+
const RPCErrorStrings = Base.IdDict(
85+
PARSE_ERROR => "ParseError",
86+
INVALID_REQUEST => "InvalidRequest",
87+
METHOD_NOT_FOUND => "MethodNotFound",
88+
INVALID_PARAMS => "InvalidParams",
89+
INTERNAL_ERROR => "InternalError",
90+
[ i => "ServerError" for i in SERVER_ERROR_START:SERVER_ERROR_END]...,
91+
-32002 => "ServerNotInitialized",
92+
-32001 => "UnknownErrorCode",
93+
)
94+
795
function Base.showerror(io::IO, ex::JSONRPCError)
8-
error_code_as_string = if ex.code == -32700
9-
"ParseError"
10-
elseif ex.code == -32600
11-
"InvalidRequest"
12-
elseif ex.code == -32601
13-
"MethodNotFound"
14-
elseif ex.code == -32602
15-
"InvalidParams"
16-
elseif ex.code == -32603
17-
"InternalError"
18-
elseif ex.code == -32099
19-
"serverErrorStart"
20-
elseif ex.code == -32000
21-
"serverErrorEnd"
22-
elseif ex.code == -32002
23-
"ServerNotInitialized"
24-
elseif ex.code == -32001
25-
"UnknownErrorCode"
26-
elseif ex.code == -32800
27-
"RequestCancelled"
28-
elseif ex.code == -32801
29-
"ContentModified"
30-
else
31-
"Unkonwn"
32-
end
96+
error_code_as_string = get(RPCErrorStrings, ex.code, "Unknown")
3397

3498
print(io, error_code_as_string)
3599
print(io, ": ")

test/test_core.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
@test sprint(showerror, JSONRPC.JSONRPCError(-32601, "FOO", "BAR")) == "MethodNotFound: FOO (BAR)"
55
@test sprint(showerror, JSONRPC.JSONRPCError(-32602, "FOO", "BAR")) == "InvalidParams: FOO (BAR)"
66
@test sprint(showerror, JSONRPC.JSONRPCError(-32603, "FOO", "BAR")) == "InternalError: FOO (BAR)"
7-
@test sprint(showerror, JSONRPC.JSONRPCError(-32099, "FOO", "BAR")) == "serverErrorStart: FOO (BAR)"
8-
@test sprint(showerror, JSONRPC.JSONRPCError(-32000, "FOO", "BAR")) == "serverErrorEnd: FOO (BAR)"
97
@test sprint(showerror, JSONRPC.JSONRPCError(-32002, "FOO", "BAR")) == "ServerNotInitialized: FOO (BAR)"
108
@test sprint(showerror, JSONRPC.JSONRPCError(-32001, "FOO", "BAR")) == "UnknownErrorCode: FOO (BAR)"
11-
@test sprint(showerror, JSONRPC.JSONRPCError(-32800, "FOO", "BAR")) == "RequestCancelled: FOO (BAR)"
12-
@test sprint(showerror, JSONRPC.JSONRPCError(-32801, "FOO", "BAR")) == "ContentModified: FOO (BAR)"
13-
@test sprint(showerror, JSONRPC.JSONRPCError(1, "FOO", "BAR")) == "Unkonwn: FOO (BAR)"
9+
@test sprint(showerror, JSONRPC.JSONRPCError(1, "FOO", "BAR")) == "Unknown: FOO (BAR)"
1410
end

0 commit comments

Comments
 (0)