Skip to content

Commit

Permalink
Use invalid request handler rather than raising key error for request…
Browse files Browse the repository at this point in the history
…s after shutdown (#432)
  • Loading branch information
smacke authored Sep 8, 2023
1 parent e837c55 commit 0d86844
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
18 changes: 18 additions & 0 deletions pylsp/lsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,21 @@ class TextDocumentSyncKind:
class NotebookCellKind:
Markup = 1
Code = 2


# https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#errorCodes
class ErrorCodes:
ParseError = -32700
InvalidRequest = -32600
MethodNotFound = -32601
InvalidParams = -32602
InternalError = -32603
jsonrpcReservedErrorRangeStart = -32099
ServerNotInitialized = -32002
UnknownErrorCode = -32001
jsonrpcReservedErrorRangeEnd = -32000
lspReservedErrorRangeStart = -32899
ServerCancelled = -32802
ContentModified = -32801
RequestCancelled = -32800
lspReservedErrorRangeEnd = -32800
10 changes: 9 additions & 1 deletion pylsp/python_lsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def __getitem__(self, item):
if self._shutdown and item != "exit":
# exit is the only allowed method during shutdown
log.debug("Ignoring non-exit method during shutdown: %s", item)
raise KeyError
item = "invalid_request_after_shutdown"

try:
return super().__getitem__(item)
Expand All @@ -234,6 +234,14 @@ def m_shutdown(self, **_kwargs):
workspace.close()
self._shutdown = True

def m_invalid_request_after_shutdown(self, **_kwargs):
return {
"error": {
"code": lsp.ErrorCodes.InvalidRequest,
"message": "Requests after shutdown are not valid",
}
}

def m_exit(self, **_kwargs):
self._endpoint.shutdown()
if self._jsonrpc_stream_reader is not None:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies = [
"importlib_metadata>=4.8.3;python_version<\"3.10\"",
"jedi>=0.17.2,<0.20.0",
"pluggy>=1.0.0",
"python-lsp-jsonrpc>=1.0.0",
"python-lsp-jsonrpc>=1.1.0,<2.0.0",
"ujson>=3.0.0",
]
dynamic = ["version"]
Expand Down

0 comments on commit 0d86844

Please sign in to comment.