Skip to content

Commit 126d269

Browse files
committed
Fix: add missing awaits.
Better error reporting. Fix sendResult.
1 parent 951450c commit 126d269

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

client/src/CodeChatEditorFramework.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ class WebSocketComm {
170170
contents.source.Diff.version !==
171171
this.version
172172
) {
173+
report_error(
174+
`Out of sync: Client version ${this.version} !== incoming version ${contents.source.Diff.version}.`,
175+
);
173176
this.send_result(id, "OutOfSync");
174177
return;
175178
}

extensions/VSCode/src/extension.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ export const activate = (context: vscode.ExtensionContext) => {
327327
value as UpdateMessageContents;
328328
const doc = get_document(current_update.file_path);
329329
if (doc === undefined) {
330-
sendResult(id, {
330+
await sendResult(id, {
331331
NoOpenDocument: current_update.file_path,
332332
});
333333
break;
@@ -358,7 +358,7 @@ export const activate = (context: vscode.ExtensionContext) => {
358358
assert("Diff" in source);
359359
// If this diff was not made against the text we currently have, reject it.
360360
if (source.Diff.version !== version) {
361-
sendResult(id, "OutOfSync");
361+
await sendResult(id, "OutOfSync");
362362
// Send an `Update` with the full text to re-sync the Client.
363363
send_update(true);
364364
break;
@@ -437,7 +437,7 @@ export const activate = (context: vscode.ExtensionContext) => {
437437
),
438438
];
439439
}
440-
sendResult(id);
440+
await sendResult(id);
441441
break;
442442
}
443443

@@ -452,7 +452,7 @@ export const activate = (context: vscode.ExtensionContext) => {
452452
current_file,
453453
);
454454
} catch (e) {
455-
sendResult(id, {
455+
await sendResult(id, {
456456
OpenFileFailed: [
457457
current_file,
458458
(e as Error).toString(),
@@ -467,7 +467,7 @@ export const activate = (context: vscode.ExtensionContext) => {
467467
current_editor?.viewColumn,
468468
);
469469
ignore_active_editor_change = false;
470-
sendResult(id);
470+
await sendResult(id);
471471
} else {
472472
// TODO: open using a custom document editor.
473473
// See
@@ -489,17 +489,17 @@ export const activate = (context: vscode.ExtensionContext) => {
489489
},
490490
)
491491
.then(
492-
() => sendResult(id),
493-
(reason) =>
494-
sendResult(id, {
492+
async () => await sendResult(id),
493+
async (reason) =>
494+
await sendResult(id, {
495495
OpenFileFailed: [
496496
current_file,
497497
reason,
498498
],
499499
}),
500500
);
501501
}
502-
sendResult(id);
502+
await sendResult(id);
503503
}
504504
break;
505505
}
@@ -530,7 +530,7 @@ export const activate = (context: vscode.ExtensionContext) => {
530530
console_log(
531531
`CodeChat Editor extension: Result(LoadFile(${format_struct(load_file_result)}))`,
532532
);
533-
codeChatEditorServer.sendResultLoadfile(
533+
await codeChatEditorServer.sendResultLoadfile(
534534
id,
535535
load_file_result,
536536
);
@@ -541,7 +541,7 @@ export const activate = (context: vscode.ExtensionContext) => {
541541
const client_html = value as string;
542542
assert(webview_panel !== undefined);
543543
webview_panel.webview.html = client_html;
544-
sendResult(id);
544+
await sendResult(id);
545545
// Now that the Client is loaded, send the editor's
546546
// current file to the server.
547547
send_update(false);
@@ -576,19 +576,27 @@ export const deactivate = async () => {
576576
// Format a complex data structure as a string when in debug mode.
577577
const format_struct = (complex_data_structure: any): string =>
578578
DEBUG_ENABLED
579-
? JSON.stringify(complex_data_structure).substring(
579+
// If the struct is `undefined`, print an empty string.
580+
? JSON.stringify(complex_data_structure ?? "").substring(
580581
0,
581582
MAX_MESSAGE_LENGTH,
582583
)
583584
: "";
584585

585586
// Send a result (a response to a message from the server) back to the server.
586-
const sendResult = (id: number, result: ResultErrTypes | null = null) => {
587+
const sendResult = async (id: number, result?: ResultErrTypes) => {
587588
assert(codeChatEditorServer);
588589
console_log(
589590
`CodeChat Editor extension: sending Result(id = ${id}, ${format_struct(result)}).`,
590591
);
591-
codeChatEditorServer.sendResult(id, JSON.stringify(result));
592+
try {
593+
await codeChatEditorServer.sendResult(
594+
id,
595+
result === undefined ? undefined : JSON.stringify(result),
596+
);
597+
} catch (e) {
598+
show_error(`Error in sendResult for id ${id}: ${e}.`);
599+
}
592600
};
593601

594602
// This is called after an event such as an edit, when the CodeChat panel
@@ -614,9 +622,13 @@ const send_update = (this_is_dirty: boolean) => {
614622
console_log(
615623
`CodeChat Editor extension: sending CurrentFile(${current_file}}).`,
616624
);
617-
await codeChatEditorServer!.sendMessageCurrentFile(
618-
current_file,
619-
);
625+
try {
626+
await codeChatEditorServer!.sendMessageCurrentFile(
627+
current_file,
628+
);
629+
} catch (e) {
630+
show_error(`Error sending CurrentFile message: ${e}.`);
631+
}
620632
// Since we just requested a new file, the contents are
621633
// clean by definition.
622634
is_dirty = false;

0 commit comments

Comments
 (0)