mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-22 11:24:35 +00:00
Make server panic hook more error resilient (#12610)
This commit is contained in:
parent
2e2b1b460f
commit
27edadec29
3 changed files with 65 additions and 41 deletions
|
@ -1,6 +1,6 @@
|
|||
use std::sync::OnceLock;
|
||||
|
||||
use anyhow::Context;
|
||||
use lsp_types::notification::Notification;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use crate::server::ClientSender;
|
||||
|
||||
|
@ -10,53 +10,31 @@ pub(crate) fn init_messenger(client_sender: ClientSender) {
|
|||
MESSENGER
|
||||
.set(client_sender)
|
||||
.expect("messenger should only be initialized once");
|
||||
|
||||
// unregister any previously registered panic hook
|
||||
let _ = std::panic::take_hook();
|
||||
|
||||
// When we panic, try to notify the client.
|
||||
std::panic::set_hook(Box::new(move |panic_info| {
|
||||
if let Some(messenger) = MESSENGER.get() {
|
||||
let _ = messenger.send(lsp_server::Message::Notification(
|
||||
lsp_server::Notification {
|
||||
method: lsp_types::notification::ShowMessage::METHOD.into(),
|
||||
params: serde_json::to_value(lsp_types::ShowMessageParams {
|
||||
typ: lsp_types::MessageType::ERROR,
|
||||
message: String::from(
|
||||
"The Ruff language server exited with a panic. See the logs for more details."
|
||||
),
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
let backtrace = std::backtrace::Backtrace::force_capture();
|
||||
tracing::error!("{panic_info}\n{backtrace}");
|
||||
#[allow(clippy::print_stderr)]
|
||||
{
|
||||
// we also need to print to stderr directly in case tracing hasn't
|
||||
// been initialized.
|
||||
eprintln!("{panic_info}\n{backtrace}");
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
pub(crate) fn show_message(message: String, message_type: lsp_types::MessageType) {
|
||||
try_show_message(message, message_type).unwrap();
|
||||
}
|
||||
|
||||
pub(super) fn try_show_message(
|
||||
message: String,
|
||||
message_type: lsp_types::MessageType,
|
||||
) -> crate::Result<()> {
|
||||
MESSENGER
|
||||
.get()
|
||||
.expect("messenger should be initialized")
|
||||
.ok_or_else(|| anyhow::anyhow!("messenger not initialized"))?
|
||||
.send(lsp_server::Message::Notification(
|
||||
lsp_server::Notification {
|
||||
method: lsp_types::notification::ShowMessage::METHOD.into(),
|
||||
params: serde_json::to_value(lsp_types::ShowMessageParams {
|
||||
typ: message_type,
|
||||
message,
|
||||
})
|
||||
.unwrap(),
|
||||
})?,
|
||||
},
|
||||
))
|
||||
.expect("message should send");
|
||||
.context("Failed to send message")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sends an error to the client with a formatted message. The error is sent in a
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue