Skip to content

Commit 747cb21

Browse files
committed
Fix: better error reporting in the VSCode extension.
1 parent 22ff197 commit 747cb21

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

extensions/VSCode/src/extension.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
DEBUG_ENABLED,
4949
MAX_MESSAGE_LENGTH,
5050
} from "../../../client/src/debug_enabled.mjs";
51+
import { ResultErrTypes } from "../../../client/src/rust-types/ResultErrTypes";
5152

5253
// Globals
5354
// -----------------------------------------------------------------------------
@@ -325,10 +326,9 @@ export const activate = (context: vscode.ExtensionContext) => {
325326
value as UpdateMessageContents;
326327
const doc = get_document(current_update.file_path);
327328
if (doc === undefined) {
328-
sendResult(
329-
id,
330-
`No open document for ${current_update.file_path}`,
331-
);
329+
sendResult(id, {
330+
NoOpenDocument: current_update.file_path,
331+
});
332332
break;
333333
}
334334
if (current_update.contents !== undefined) {
@@ -357,10 +357,7 @@ export const activate = (context: vscode.ExtensionContext) => {
357357
assert("Diff" in source);
358358
// If this diff was not made against the text we currently have, reject it.
359359
if (source.Diff.version !== version) {
360-
sendResult(
361-
id,
362-
"Out of sync: incorrect version for diff.",
363-
);
360+
sendResult(id, "OutOfSync");
364361
// Send an `Update` with the full text to re-sync the Client.
365362
send_update(true);
366363
break;
@@ -454,10 +451,12 @@ export const activate = (context: vscode.ExtensionContext) => {
454451
current_file,
455452
);
456453
} catch (e) {
457-
sendResult(
458-
id,
459-
`Error: unable to open file ${current_file}: ${e}`,
460-
);
454+
sendResult(id, {
455+
OpenFileFailed: [
456+
current_file,
457+
(e as Error).toString(),
458+
],
459+
});
461460
continue;
462461
}
463462
ignore_active_editor_change = true;
@@ -491,10 +490,12 @@ export const activate = (context: vscode.ExtensionContext) => {
491490
.then(
492491
() => sendResult(id),
493492
(reason) =>
494-
sendResult(
495-
id,
496-
`Error: unable to open file ${current_file}: ${reason}`,
497-
),
493+
sendResult(id, {
494+
OpenFileFailed: [
495+
current_file,
496+
reason,
497+
],
498+
}),
498499
);
499500
}
500501
sendResult(id);
@@ -581,12 +582,12 @@ const format_struct = (complex_data_structure: any): string =>
581582
: "";
582583

583584
// Send a result (a response to a message from the server) back to the server.
584-
const sendResult = (id: number, result: string | null = null) => {
585+
const sendResult = (id: number, result: ResultErrTypes | null = null) => {
585586
assert(codeChatEditorServer);
586587
console_log(
587588
`CodeChat Editor extension: sending Result(id = ${id}, ${format_struct(result)}).`,
588589
);
589-
codeChatEditorServer.sendResult(id, result);
590+
codeChatEditorServer.sendResult(id, JSON.stringify(result));
590591
};
591592

592593
// This is called after an event such as an edit, when the CodeChat panel

extensions/VSCode/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,18 @@ impl CodeChatEditorServer {
9999
pub async fn send_result(
100100
&self,
101101
id: f64,
102+
// If provided, a JSON-encoded `ResultErrTypes`.
102103
message_result: Option<String>,
103104
) -> std::io::Result<()> {
104-
self.0.send_result(id, message_result).await
105+
let message = if let Some(err_json) = message_result {
106+
match serde_json::from_str(&err_json) {
107+
Ok(v) => Some(v),
108+
Err(err) => return Err(std::io::Error::other(err.to_string())),
109+
}
110+
} else {
111+
None
112+
};
113+
self.0.send_result(id, message).await
105114
}
106115

107116
#[napi]

server/src/ide.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,13 @@ impl CodeChatEditorServer {
277277
pub async fn send_result(
278278
&self,
279279
id: f64,
280-
message_result: Option<String>,
280+
message_result: Option<ResultErrTypes>,
281281
) -> std::io::Result<()> {
282282
let editor_message = EditorMessage {
283283
id,
284284
message: webserver::EditorMessageContents::Result(
285285
if let Some(message_result) = message_result {
286-
Err(ResultErrTypes::ExtensionError(message_result))
286+
Err(message_result)
287287
} else {
288288
Ok(ResultOkTypes::Void)
289289
},

server/src/webserver.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,12 @@ pub enum ResultErrTypes {
297297
SaveFileError(PathBuf, String),
298298
#[error("unable to watch file '{0}': {1}")]
299299
FileWatchError(PathBuf, String),
300-
#[error("IDE error: {0}")]
301-
ExtensionError(String),
302300
#[error("ignoring update for {0} because it's not the current file {1}")]
303301
IgnoredUpdate(String, String),
302+
#[error("no open document for {0}")]
303+
NoOpenDocument(String),
304+
#[error("unable to open file {0}: {1}")]
305+
OpenFileFailed(String, String),
304306
}
305307

306308
/// Specify the type of IDE that this client represents.

0 commit comments

Comments
 (0)