mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 04:23:25 +00:00
Add panic_context module for better panic messages
This commit is contained in:
parent
e315fd9bb0
commit
d3a2b21a8c
3 changed files with 55 additions and 2 deletions
|
@ -1,5 +1,5 @@
|
||||||
//! A visitor for downcasting arbitrary request (JSON) into a specific type.
|
//! A visitor for downcasting arbitrary request (JSON) into a specific type.
|
||||||
use std::panic;
|
use std::{fmt, panic};
|
||||||
|
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
|
@ -49,7 +49,7 @@ impl<'a> RequestDispatcher<'a> {
|
||||||
) -> Result<&mut Self>
|
) -> Result<&mut Self>
|
||||||
where
|
where
|
||||||
R: lsp_types::request::Request + 'static,
|
R: lsp_types::request::Request + 'static,
|
||||||
R::Params: DeserializeOwned + Send + 'static,
|
R::Params: DeserializeOwned + Send + fmt::Debug + 'static,
|
||||||
R::Result: Serialize + 'static,
|
R::Result: Serialize + 'static,
|
||||||
{
|
{
|
||||||
let (id, params) = match self.parse::<R>() {
|
let (id, params) = match self.parse::<R>() {
|
||||||
|
@ -61,7 +61,10 @@ impl<'a> RequestDispatcher<'a> {
|
||||||
|
|
||||||
self.global_state.task_pool.handle.spawn({
|
self.global_state.task_pool.handle.spawn({
|
||||||
let world = self.global_state.snapshot();
|
let world = self.global_state.snapshot();
|
||||||
|
|
||||||
move || {
|
move || {
|
||||||
|
let _ctx =
|
||||||
|
stdx::panic_context::enter(format!("request: {} {:#?}", R::METHOD, params));
|
||||||
let result = f(world, params);
|
let result = f(world, params);
|
||||||
Task::Response(result_to_response::<R>(id, result))
|
Task::Response(result_to_response::<R>(id, result))
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
mod macros;
|
mod macros;
|
||||||
|
pub mod panic_context;
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn is_ci() -> bool {
|
pub fn is_ci() -> bool {
|
||||||
|
|
49
crates/stdx/src/panic_context.rs
Normal file
49
crates/stdx/src/panic_context.rs
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
//! A micro-crate to enhance panic messages with context info.
|
||||||
|
//!
|
||||||
|
//! FIXME: upstream to https://github.com/kriomant/panic-context ?
|
||||||
|
|
||||||
|
use std::{cell::RefCell, panic, sync::Once};
|
||||||
|
|
||||||
|
pub fn enter(context: String) -> impl Drop {
|
||||||
|
static ONCE: Once = Once::new();
|
||||||
|
ONCE.call_once(PanicContext::init);
|
||||||
|
|
||||||
|
with_ctx(|ctx| ctx.push(context));
|
||||||
|
PanicContext { _priv: () }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
struct PanicContext {
|
||||||
|
_priv: (),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PanicContext {
|
||||||
|
fn init() {
|
||||||
|
let default_hook = panic::take_hook();
|
||||||
|
let hook = move |panic_info: &panic::PanicInfo<'_>| {
|
||||||
|
with_ctx(|ctx| {
|
||||||
|
if !ctx.is_empty() {
|
||||||
|
eprintln!("Panic context:");
|
||||||
|
for frame in ctx.iter() {
|
||||||
|
eprintln!("> {}\n", frame)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default_hook(panic_info)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
panic::set_hook(Box::new(hook))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for PanicContext {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
with_ctx(|ctx| assert!(ctx.pop().is_some()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn with_ctx(f: impl FnOnce(&mut Vec<String>)) {
|
||||||
|
thread_local! {
|
||||||
|
static CTX: RefCell<Vec<String>> = RefCell::new(Vec::new());
|
||||||
|
}
|
||||||
|
CTX.with(|ctx| f(&mut *ctx.borrow_mut()))
|
||||||
|
}
|
Loading…
Reference in a new issue