Skip to content

Commit 846e254

Browse files
committed
Fix: better error reporting
Add units to variable names.
1 parent 7db5ba3 commit 846e254

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

client/src/CodeChatEditor.mts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ export const page_init = () => {
187187
on_navigate,
188188
);
189189
document.addEventListener("click", on_click);
190+
// Provide basic error reporting for uncaught errors. However, enabling either of these makes the Client not work. ???
191+
//window.addEventListener("unhandledrejection", on_error);
192+
//window.addEventListener("error", on_error);
190193

191194
window.CodeChatEditor = {
192195
open_lp,
@@ -204,7 +207,7 @@ export const set_is_dirty = (value: boolean = true) => {
204207

205208
// This is copied
206209
// from[MDN](https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event#checking_whether_loading_is_already_complete).
207-
const on_dom_content_loaded = (on_load_func: () => void) => {
210+
export const on_dom_content_loaded = (on_load_func: () => void) => {
208211
if (document.readyState === "loading") {
209212
// Loading hasn't finished yet.
210213
document.addEventListener("DOMContentLoaded", on_load_func);
@@ -553,6 +556,8 @@ const save_then_navigate = (codeChatEditorUrl: URL) => {
553556
});
554557
};
555558

559+
// This can be called by the framework. Therefore, make no assumptions
560+
// about variables being valid; it be called before a file is loaded, etc.
556561
const scroll_to_line = (line: number) => {
557562
if (is_doc_only()) {
558563
// TODO.
@@ -567,6 +572,19 @@ export const console_log = (...args: any) => {
567572
}
568573
};
569574

575+
// A global error handler: this is called on any uncaught exception.
576+
export const on_error = (event: Event) => {
577+
let err_str: string;
578+
if (event instanceof ErrorEvent) {
579+
err_str = `${event.filename}:${event.lineno}: ${event.message}`;
580+
} else if (event instanceof PromiseRejectionEvent) {
581+
err_str = `${event.promise} rejected: ${event.reason}`;
582+
} else {
583+
err_str = `Unexpected error ${typeof(event)}: ${event}`
584+
}
585+
show_toast(`Error: ${err_str}`);
586+
};
587+
570588
// Testing
571589
// -------
572590
//

client/src/CodeChatEditorFramework.mts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ import {
4141
MessageResult,
4242
UpdateMessageContents,
4343
} from "./shared_types.mjs";
44-
import { console_log } from "./CodeChatEditor.mjs";
44+
import {
45+
console_log,
46+
on_error,
47+
on_dom_content_loaded,
48+
} from "./CodeChatEditor.mjs";
4549

4650
// Websocket
4751
// ---------
@@ -51,8 +55,8 @@ import { console_log } from "./CodeChatEditor.mjs";
5155
//
5256
// The max length of a message to show in the console.
5357
const MAX_MESSAGE_LENGTH = 200;
54-
// The timeout for a websocket `Response`.
55-
const RESPONSE_TIMEOUT = 15000;
58+
// The timeout for a websocket `Response`, in ms.
59+
const RESPONSE_TIMEOUT_MS = 1500000;
5660

5761
// An instance of the websocket communication class.
5862
let webSocketComm: WebSocketComm;
@@ -141,10 +145,10 @@ class WebSocketComm {
141145
const cursor_position = current_update.cursor_position;
142146
if (contents !== undefined) {
143147
if (
144-
root_iframe!.contentDocument?.readyState !==
145-
"loading"
148+
root_iframe!.contentDocument?.readyState ===
149+
"complete"
146150
) {
147-
// The page is ready; load in the provided content.
151+
// Wait until after the DOM is ready, since we rely on content set in `on_dom_content_loaded` in the Client.
148152
this.promise = this.promise.finally(
149153
async () =>
150154
await set_content(
@@ -293,7 +297,7 @@ class WebSocketComm {
293297
this.pending_messages[id] = {
294298
timer_id: window.setTimeout(
295299
this.report_server_timeout,
296-
RESPONSE_TIMEOUT,
300+
RESPONSE_TIMEOUT_MS,
297301
id,
298302
),
299303
callback,
@@ -384,6 +388,10 @@ export const page_init = (
384388
) => {
385389
testMode = testMode_;
386390
on_dom_content_loaded(() => {
391+
// Provide basic error reporting for uncaught errors.
392+
window.addEventListener("unhandledrejection", on_error);
393+
window.addEventListener("error", on_error);
394+
387395
// If the hosting page uses HTTPS, then use a secure websocket (WSS
388396
// protocol); otherwise, use an insecure websocket (WS).
389397
const protocol = window.location.protocol === "http:" ? "ws:" : "wss:";
@@ -400,18 +408,6 @@ export const page_init = (
400408
});
401409
};
402410

403-
// This is copied from
404-
// [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event#checking_whether_loading_is_already_complete).
405-
const on_dom_content_loaded = (on_load_func: () => void) => {
406-
if (document.readyState === "loading") {
407-
// Loading hasn't finished yet.
408-
document.addEventListener("DOMContentLoaded", on_load_func);
409-
} else {
410-
// `DOMContentLoaded` has already fired.
411-
on_load_func();
412-
}
413-
};
414-
415411
// Tell TypeScript about the global namespace this program defines.
416412
declare global {
417413
interface Window {

client/src/assert.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// `assert.mts`
22
// ============
3-
// Provide a simple `assert` function to check conditions at runtime. Using things like [assert](https://nodejs.org/api/assert.html) causes problems -- somehow, this indicates that the code in running in a development environment (see [this](https://github.com/micromark/micromark/issues/87#issuecomment-908924233)).
3+
// Provide a simple `assert` function to check conditions at runtime. Using things like [assert](https://nodejs.org/api/assert.html) causes problems -- somehow, this indicates that the code is running in a development environment (see [this](https://github.com/micromark/micromark/issues/87#issuecomment-908924233)).
44
//
55
// Taken from the TypeScript
66
// [docs](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#assertion-functions).

extensions/VSCode/src/extension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ enum CodeChatEditorClientLocation {
4949
}
5050
// The max length of a message to show in the console.
5151
const MAX_MESSAGE_LENGTH = 200;
52-
// The timeout for a websocket `Response`.
53-
const RESPONSE_TIMEOUT = 15000;
52+
// The timeout for a websocket `Response`, in ms.
53+
const RESPONSE_TIMEOUT_MS = 1500000;
5454
// True to enable additional debug logging.
5555
const DEBUG_ENABLED = false;
5656

@@ -622,7 +622,7 @@ const send_message = (
622622
);
623623
websocket.send(JSON.stringify(jm));
624624
pending_messages[id] = {
625-
timer_id: setTimeout(report_server_timeout, RESPONSE_TIMEOUT, id),
625+
timer_id: setTimeout(report_server_timeout, RESPONSE_TIMEOUT_MS, id),
626626
callback,
627627
};
628628
};

server/src/webserver.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,9 @@ macro_rules! queue_send {
341341

342342
/// Globals
343343
/// -------
344-
// The timeout for a reply from a websocket. Use a short timeout to speed up
344+
// The timeout for a reply from a websocket, in ms. Use a short timeout to speed up
345345
// unit tests.
346-
const REPLY_TIMEOUT: Duration = if cfg!(test) {
346+
const REPLY_TIMEOUT_MS: Duration = if cfg!(test) {
347347
Duration::from_millis(500)
348348
} else {
349349
Duration::from_millis(15000)
@@ -1246,7 +1246,7 @@ pub async fn client_websocket(
12461246
_ => {
12471247
let timeout_tx = from_websocket_tx.clone();
12481248
let waiting_task = actix_rt::spawn(async move {
1249-
sleep(REPLY_TIMEOUT).await;
1249+
sleep(REPLY_TIMEOUT_MS).await;
12501250
let msg = format!("Timeout: message id {} unacknowledged.", m.id);
12511251
error!("{msg}");
12521252
// Since the websocket failed to send a

0 commit comments

Comments
 (0)