Skip to content

Commit 57fa458

Browse files
committed
internal: nicer error reporting
1 parent 3e52942 commit 57fa458

File tree

5 files changed

+24
-10
lines changed

5 files changed

+24
-10
lines changed

crates/flycheck/src/json_workspace.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ pub struct JsonWorkspaceHandle {
4646
#[derive(Debug, Clone, Deserialize, Serialize)]
4747
#[serde(tag = "type")]
4848
pub enum DiscoverProjectMessage {
49-
Error { message: String },
49+
Error { message: String, context: Option<String> },
5050
Progress { message: String },
5151
Finished { project_json: Vec<ProjectJsonData> },
5252
}
5353

5454
impl ParseFromLine for DiscoverProjectMessage {
5555
fn from_line(line: &str, _error: &mut String) -> Option<Self> {
5656
let Ok(value) = serde_json::from_str::<serde_json::Value>(line) else {
57-
return Some(DiscoverProjectMessage::Error { message: line.to_owned() });
57+
return Some(DiscoverProjectMessage::Error { message: line.to_owned(), context: None });
5858
};
5959

6060
if let Ok(project) = serde_json::from_value::<ProjectJsonData>(value.clone()) {
@@ -67,6 +67,15 @@ impl ParseFromLine for DiscoverProjectMessage {
6767
});
6868
}
6969

70+
if let Some(error) = value.pointer("/fields/error") {
71+
if let Some(source) = value.pointer("/fields/source") {
72+
return Some(DiscoverProjectMessage::Error {
73+
message: error.as_str().unwrap().to_owned(),
74+
context: Some(source.as_str().unwrap().to_owned()),
75+
});
76+
}
77+
}
78+
7079
None
7180
}
7281

crates/rust-analyzer/src/lsp/utils.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,19 @@ impl GlobalState {
8080
match additional_info {
8181
Some(additional_info) => {
8282
tracing::error!("{}:\n{}", &message, &additional_info);
83+
self.show_message(
84+
lsp_types::MessageType::ERROR,
85+
message,
86+
tracing::enabled!(tracing::Level::ERROR),
87+
);
8388
}
8489
None => {
8590
tracing::error!("{}", &message);
91+
self.send_notification::<lsp_types::notification::ShowMessage>(
92+
lsp_types::ShowMessageParams { typ: lsp_types::MessageType::ERROR, message },
93+
);
8694
}
8795
}
88-
self.show_message(
89-
lsp_types::MessageType::ERROR,
90-
message,
91-
tracing::enabled!(tracing::Level::ERROR),
92-
);
9396
}
9497

9598
/// rust-analyzer is resilient -- if it fails, this doesn't usually affect

crates/rust-analyzer/src/main_loop.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,9 @@ impl GlobalState {
794794
flycheck::DiscoverProjectMessage::Progress { message } => {
795795
self.report_progress("Buck", Progress::Report, Some(message), None, None)
796796
}
797-
flycheck::DiscoverProjectMessage::Error { message } => {
798-
self.show_and_log_error(message.clone(), None);
797+
flycheck::DiscoverProjectMessage::Error { message, context } => {
798+
let message = format!("Project discovery failed: {message}");
799+
self.show_and_log_error(message.clone(), context);
799800
self.report_progress("Buck", Progress::End, Some(message), None, None)
800801
}
801802
}

crates/rust-analyzer/src/reload.rs

+1
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ impl GlobalState {
629629
let Some((last_op_result, _)) = self.fetch_workspaces_queue.last_op_result() else {
630630
return Ok(());
631631
};
632+
632633
if last_op_result.is_empty() {
633634
stdx::format_to!(buf, "rust-analyzer failed to discover workspace");
634635
} else {

docs/dev/lsp-extensions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!---
2-
lsp/ext.rs hash: 422dcc22c2e56166
2+
lsp/ext.rs hash: 7f4bb9a6d64818e1
33
44
If you need to change the above hash to make the test pass, please check if you
55
need to adjust this doc as well and ping this issue:

0 commit comments

Comments
 (0)