Skip to content

Commit 2a64999

Browse files
committed
Allow other applications to hook into showerror
1 parent 5adc04b commit 2a64999

File tree

1 file changed

+70
-25
lines changed

1 file changed

+70
-25
lines changed

src/core.jl

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,80 @@
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+
PARSE_ERROR
21+
22+
Invalid JSON was received by the server.
23+
An error occurred on the server while parsing the JSON text.
24+
"""
25+
const PARSE_ERROR = -32700
26+
27+
"""
28+
INVALID_REQUEST
29+
30+
The JSON sent is not a valid Request object.
31+
"""
32+
const INVALID_REQUEST = -32600
33+
34+
"""
35+
METHOD_NOT_FOUND
36+
37+
The method does not exist / is not available.
38+
"""
39+
const METHOD_NOT_FOUND = -32700
40+
41+
"""
42+
INVALID_PARAMS
43+
44+
Invalid method parameter(s).
45+
"""
46+
const INVALID_PARAMS = -32700
47+
48+
"""
49+
INTERNAL_ERROR
50+
51+
Internal JSON-RPC error.
52+
"""
53+
const INTERNAL_ERROR = -32700
54+
55+
"""
56+
RPCErrorStrings
57+
58+
A `Base.IdDict` containing the mapping of JSON-RPC error codes to a short, descriptive string.
59+
60+
Use this to hook into `showerror(io::IO, ::JSONRPCError)` for display purposes. A default fallback to `"Unknown"` exists.
61+
"""
62+
const RPCErrorStrings = Base.IdDict(
63+
PARSE_ERROR => "ParseError",
64+
INVALID_REQUEST => "InvalidRequest",
65+
METHOD_NOT_FOUND => "MethodNotFound",
66+
INVALID_PARAMS => "InvalidParams",
67+
INTERNAL_ERROR => "InternalError",
68+
[ i => "ServerError" for i in -32099:-32000]...,
69+
# TODO: these should be removed - they are in the application/implementation specific range
70+
-32002 => "ServerNotInitialized",
71+
-32001 => "UnknownErrorCode",
72+
-32800 => "RequestCancelled",
73+
-32801 => "ContentModified"
74+
)
75+
776
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
77+
error_code_as_string = get(RPCErrorStrings, ex.code, "Unknown")
3378

3479
print(io, error_code_as_string)
3580
print(io, ": ")

0 commit comments

Comments
 (0)