Skip to content

Commit 53b4b84

Browse files
committed
Refactor Error Code definitions
This refactors the way error codes are handled, in preparation for making JSONRPC.jl less LSP specific, by hooking into custom error printing provided by an upstream PR.
1 parent e67befb commit 53b4b84

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

src/LanguageServer.jl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,69 @@ using .URIs2
1717

1818
JSON.lower(uri::URI) = string(uri)
1919

20+
# these are the endpoints of JSON-RPC error codes
21+
# per the LSP spec, no LSP error codes should lie
22+
# between these
23+
const SERVER_ERROR_END = -32000
24+
const SERVER_ERROR_START = -32099
25+
26+
# These are existing reserved errors for JSONRPC.jl
27+
# We shouldn't throw these manually
28+
const SERVER_NOT_INITIALIZED = -32002
29+
const UNKNOWN_ERROR_CODE = -32001
30+
31+
# LSP specific error codes
32+
# these are the defined ranges for LSP errors
33+
# not real error codes. Custom errors must lie
34+
# outside this range
35+
const LSP_RESERVED_ERROR_START = -32899
36+
const LSP_RESERVED_ERROR_END = -32800
37+
38+
const REQUEST_CANCELLED = -32800
39+
const CONTENT_MODIFIED = -32801
40+
const SERVER_CANCELLED = -32802
41+
const REQUEST_FAILED = -32803
42+
43+
# Specific to our implementation
44+
const NO_DOCUMENT = -33100
45+
const MISMATCHED_VERSION = -33101
46+
const SHUTDOWN_REQUEST = -32600
47+
48+
const ERROR_CODES = (
49+
REQUEST_CANCELLED,
50+
CONTENT_MODIFIED,
51+
SERVER_CANCELLED,
52+
REQUEST_FAILED,
53+
NO_DOCUMENT,
54+
MISMATCHED_VERSION,
55+
SHUTDOWN_REQUEST
56+
)
57+
58+
function __init__()
59+
# init JSONRPC error messages
60+
conflicting_codes = filter(ERROR_CODES) do code
61+
!haskey(JSONRPC.JSONRPCErrorStrings, code) && return false
62+
return JSONRPC.JSONRPCErrorStrings[code] != "ServerError"
63+
end
64+
# if any of the codes we want to use are already set,
65+
# it means we have a conflict with another application
66+
# using JSONRPC.jl at the same time in this process
67+
# warn the user of this, so that they can debug/work around it
68+
if !isempty(conflicting_codes)
69+
@warn """JSONRPC Error Codes conflict!
70+
71+
Another library besides LanguageServer.jl is using JSONRPC.jl with conflicting error codes.
72+
LanguageServer.jl will overwrite this with its own state. Faulty behavior/error printing may arise.
73+
""" Codes=conflicting_codes
74+
end
75+
76+
JSONRPC.RPCErrorStrings[REQUEST_CANCELLED] = "RequestCancelled"
77+
JSONRPC.RPCErrorStrings[CONTENT_MODIFIED] = "ContentModified"
78+
JSONRPC.RPCErrorStrings[SERVER_CANCELLED] = "ServerCancelled"
79+
JSONRPC.RPCErrorStrings[REQUEST_FAILED] = "RequestFailed"
80+
nothing
81+
end
82+
2083
include("exception_types.jl")
2184
include("protocol/protocol.jl")
2285
include("extensions/extensions.jl")

src/languageserverinstance.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ function request_wrapper(func, server::LanguageServerInstance)
257257
# it's fine to always return a value here, even for notifications, because
258258
# JSONRPC discards it anyways in that case
259259
return JSONRPC.JSONRPCError(
260-
-32600,
260+
SHUTDOWN_REQUEST,
261261
"LS shutdown was requested.",
262262
nothing
263263
)

src/utilities.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
function nodocument_error(uri, data=nothing)
55
return JSONRPC.JSONRPCError(
6-
-33100,
6+
NO_DOCUMENT,
77
"document $(uri) requested but not present in the JLS",
88
data
99
)
1010
end
1111

1212
function mismatched_version_error(uri, doc, params, msg, data=nothing)
1313
return JSONRPC.JSONRPCError(
14-
-33101,
14+
MISMATCHED_VERSION,
1515
"version mismatch in $(msg) request for $(uri): JLS $(get_version(doc)), client: $(params.version)",
1616
data
1717
)

0 commit comments

Comments
 (0)