on master help sign in
LSP: Send replies for malformed and unhandled RPC requests (#6058)

Previously we did not respond to malformed or unhandled LSP requests. The JSONRPC spec says that all non-notification requests must have responses: > When a rpc call is made, the Server MUST reply with a Response, > except for in the case of Notifications (Note that Helix is the "Server" in this case. Also from the spec: "The Server is defined as the origin of Response objects and the handler of Request objects.") So this change sends error replies for requests which can't be parsed or handled. Request IDs are also now added to the log messages for unhandled requests.

commit170593161cb23edc433d937eb62a9b8fd76bd339 authorMichael Davis <mcarsondavis@gmail.com> dateTue 7 Mar 2023, 19:50 (4 years ago) parent563ac1a
1 file changed+21−13
helix-term/src/application.rs+21−13
helix-term/src/application.rs+21−13
@@ -928,24 +928,32 @@ impl Application {
928928 Call::MethodCall(helix_lsp::jsonrpc::MethodCall {
929929 method, params, id, ..
930930 }) => {
931− let call = match MethodCall::parse(&method, params) {
932− Ok(call) => call,
931+ let reply = match MethodCall::parse(&method, params) {
933932 Err(helix_lsp::Error::Unhandled) => {
934− error!("Language Server: Method not found {}", method);
935− return;
933+ error!(
934+ "Language Server: Method {} not found in request {}",
935+ method, id
936+ );
937+ Err(helix_lsp::jsonrpc::Error {
938+ code: helix_lsp::jsonrpc::ErrorCode::MethodNotFound,
939+ message: format!("Method not found: {}", method),
940+ data: None,
941+ })
936942 }
937943 Err(err) => {
938944 log::error!(
939− "received malformed method call from Language Server: {}: {}",
945+ "Language Server: Received malformed method call {} in request {}: {}",
940946 method,
947+ id,
941948 err
942949 );
943− return;
950+ Err(helix_lsp::jsonrpc::Error {
951+ code: helix_lsp::jsonrpc::ErrorCode::ParseError,
952+ message: format!("Malformed method call: {}", method),
953+ data: None,
954+ })
944955 }
945− };
946−
947− let reply = match call {
948− MethodCall::WorkDoneProgressCreate(params) => {
956+ Ok(MethodCall::WorkDoneProgressCreate(params)) => {
949957 self.lsp_progress.create(server_id, params.token);
950958
951959 let editor_view = self
@@ -959,7 +967,7 @@ impl Application {
959967
960968 Ok(serde_json::Value::Null)
961969 }
962− MethodCall::ApplyWorkspaceEdit(params) => {
970+ Ok(MethodCall::ApplyWorkspaceEdit(params)) => {
963971 apply_workspace_edit(
964972 &mut self.editor,
965973 helix_lsp::OffsetEncoding::Utf8,
@@ -972,13 +980,13 @@ impl Application {
972980 failed_change: None,
973981 }))
974982 }
975− MethodCall::WorkspaceFolders => {
983+ Ok(MethodCall::WorkspaceFolders) => {
976984 let language_server =
977985 self.editor.language_servers.get_by_id(server_id).unwrap();
978986
979987 Ok(json!(language_server.workspace_folders()))
980988 }
981− MethodCall::WorkspaceConfiguration(params) => {
989+ Ok(MethodCall::WorkspaceConfiguration(params)) => {
982990 let result: Vec<_> = params
983991 .items
984992 .iter()