Skip to content

Commit

Permalink
fix(lsp): properly reply with error messages
Browse files Browse the repository at this point in the history
If an error happens in the request handler, return a proper error to
back to the client instead of shutting down the whole server.
  • Loading branch information
dnaka91 committed Dec 13, 2023
1 parent 4f00e12 commit 740c46d
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions crates/stef-lsp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{collections::HashMap, net::Ipv4Addr, time::Duration};
use anyhow::{bail, ensure, Context, Result};
use line_index::{LineIndex, TextRange};
use log::{as_debug, as_display, debug, error, info, warn};
use lsp_server::{Connection, Message, Notification, Request, RequestId, Response};
use lsp_server::{Connection, ErrorCode, Message, Notification, Request, RequestId, Response};
use lsp_types::{
notification::{
DidChangeConfiguration, DidChangeTextDocument, DidCloseTextDocument, DidOpenTextDocument,
Expand Down Expand Up @@ -426,15 +426,22 @@ fn main_loop(conn: &Connection, mut server: impl LanguageServer) -> Result<()> {
}
SemanticTokensFullRequest::METHOD => {
let (id, params) = cast_req::<SemanticTokensFullRequest>(req)?;
let result = server.semantic_tokens_full(params)?;
let result = server.semantic_tokens_full(params);

conn.sender.send(
Response::new_ok(
id,
result.unwrap_or(SemanticTokensResult::Tokens(
SemanticTokens::default(),
)),
)
match result {
Ok(value) => Response::new_ok(
id,
value.unwrap_or(SemanticTokensResult::Tokens(
SemanticTokens::default(),
)),
),
Err(e) => Response::new_err(
id,
ErrorCode::InternalError as _,
e.to_string(),
),
}
.into(),
)?;
}
Expand Down

0 comments on commit 740c46d

Please sign in to comment.