Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

watchdog: Handle exception in `callback`.

Prakhar Pratyushc539b8ee46ba
diff --git a/web/src/watchdog.ts b/web/src/watchdog.ts
index eeff824ba13..07082fa0faf 100644
--- a/web/src/watchdog.ts
+++ b/web/src/watchdog.ts
@@ -1,3 +1,5 @@
+import * as blueslip from "./blueslip";
+
const unsuspend_callbacks: (() => void)[] = [];
let watchdog_time = Date.now();
@@ -35,7 +37,17 @@ export function check_for_unsuspend(): void {
// Our app's JS wasn't running, which probably means the machine was
// asleep.
for (const callback of unsuspend_callbacks) {
- callback();
+ try {
+ callback();
+ } catch (error) {
+ blueslip.error(
+ `Error while executing callback '${
+ callback.name || "Anonymous function"
+ }' from unsuspend_callbacks.`,
+ undefined,
+ error,
+ );
+ }
}
}
watchdog_time = new_time;
diff --git a/web/tests/watchdog.test.js b/web/tests/watchdog.test.js
index 662d26127c1..4257bf6d56f 100644
--- a/web/tests/watchdog.test.js
+++ b/web/tests/watchdog.test.js
@@ -6,6 +6,7 @@ const MockDate = require("mockdate");
const {set_global, zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test");
+const blueslip = require("./lib/zblueslip");
let time = 0;
let checker;
@@ -58,6 +59,20 @@ run_test("basics", () => {
advance_secs(21);
watchdog.check_for_unsuspend();
assert.equal(num_times_called_back, 1);
+
+ // Error while executing callback
+ num_times_called_back = 0;
+ advance_secs(21);
+ watchdog.on_unsuspend(() => {
+ num_times_called_back += 1;
+ throw new Error("Some error while executing");
+ });
+ blueslip.expect(
+ "error",
+ `Error while executing callback 'Anonymous function' from unsuspend_callbacks.`,
+ );
+ watchdog.check_for_unsuspend();
+ assert.equal(num_times_called_back, 2);
});
run_test("suspect_offline", () => {