Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Error Code definitions #1270

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/LanguageServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,71 @@ using .URIs2

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

# these are the endpoints of JSON-RPC error codes
# per the LSP spec, no LSP error codes should lie
# between these
const SERVER_ERROR_END = -32000
const SERVER_ERROR_START = -32099

# These are existing reserved errors for JSONRPC.jl
# We shouldn't throw these manually
const SERVER_NOT_INITIALIZED = -32002
const UNKNOWN_ERROR_CODE = -32001

# LSP specific error codes
# these are the defined ranges for LSP errors
# not real error codes. Custom errors must lie
# outside this range
const LSP_RESERVED_ERROR_START = -32899
const LSP_RESERVED_ERROR_END = -32800

const REQUEST_CANCELLED = -32800
const CONTENT_MODIFIED = -32801
const SERVER_CANCELLED = -32802
const REQUEST_FAILED = -32803

# Specific to our implementation
const FORMATTING_FAILED = -33000
const NO_DOCUMENT = -33100
const MISMATCHED_VERSION = -33101
const SHUTDOWN_REQUEST = -32600

const ERROR_CODES = (
REQUEST_CANCELLED,
CONTENT_MODIFIED,
SERVER_CANCELLED,
REQUEST_FAILED,
FORMATTING_FAILED,
NO_DOCUMENT,
MISMATCHED_VERSION,
SHUTDOWN_REQUEST
)

function __init__()
# init JSONRPC error messages
conflicting_codes = filter(ERROR_CODES) do code
!haskey(JSONRPC.JSONRPCErrorStrings, code) && return false
return JSONRPC.JSONRPCErrorStrings[code] != "ServerError"
end
# if any of the codes we want to use are already set,
# it means we have a conflict with another application
# using JSONRPC.jl at the same time in this process
# warn the user of this, so that they can debug/work around it
if !isempty(conflicting_codes)
@warn """JSONRPC Error Codes conflict!

Another library besides LanguageServer.jl is using JSONRPC.jl with conflicting error codes.
LanguageServer.jl will overwrite this with its own state. Faulty behavior/error printing may arise.
""" Codes=conflicting_codes
end

JSONRPC.RPCErrorStrings[REQUEST_CANCELLED] = "RequestCancelled"
JSONRPC.RPCErrorStrings[CONTENT_MODIFIED] = "ContentModified"
JSONRPC.RPCErrorStrings[SERVER_CANCELLED] = "ServerCancelled"
JSONRPC.RPCErrorStrings[REQUEST_FAILED] = "RequestFailed"
nothing
end

include("exception_types.jl")
include("protocol/protocol.jl")
include("extensions/extensions.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/languageserverinstance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function request_wrapper(func, server::LanguageServerInstance)
# it's fine to always return a value here, even for notifications, because
# JSONRPC discards it anyways in that case
return JSONRPC.JSONRPCError(
-32600,
SHUTDOWN_REQUEST,
"LS shutdown was requested.",
nothing
)
Expand Down
4 changes: 2 additions & 2 deletions src/requests/features.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function textDocument_formatting_request(params::DocumentFormattingParams, serve
format_text(get_text(doc), params, config)
catch err
return JSONRPC.JSONRPCError(
-32000,
FORMATTING_FAILED,
"Failed to format document: $err.",
nothing
)
Expand Down Expand Up @@ -210,7 +210,7 @@ function textDocument_range_formatting_request(params::DocumentRangeFormattingPa
format_text(text_marked, params, config)
catch err
return JSONRPC.JSONRPCError(
-33000,
FORMATTING_FAILED,
"Failed to format document: $err.",
nothing
)
Expand Down
4 changes: 2 additions & 2 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

function nodocument_error(uri, data=nothing)
return JSONRPC.JSONRPCError(
-33100,
NO_DOCUMENT,
"document $(uri) requested but not present in the JLS",
data
)
end

function mismatched_version_error(uri, doc, params, msg, data=nothing)
return JSONRPC.JSONRPCError(
-33101,
MISMATCHED_VERSION,
"version mismatch in $(msg) request for $(uri): JLS $(get_version(doc)), client: $(params.version)",
data
)
Expand Down
Loading