diff --git a/README.md b/README.md index 12e1708..b967ef4 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,8 @@ requires one whenever NATS is reachable off-loopback. await client.get(uri, ref) // fetch (ref = branch/commit hash to pin a version) await client.diff(uri, newTemplate) // semantic diff: stored version vs. your draft await client.publish(uri, template, slots) // publish a new version + notify subscribers -client.subscribe(uri, onChange) // be notified the moment the prompt changes +client.subscribe(uri, onChange, { onError }) // be notified the moment the prompt changes; + // onChange errors go to onError (default console.error) ``` ## Live updates diff --git a/client.js b/client.js index dc1f694..60fc260 100644 --- a/client.js +++ b/client.js @@ -187,7 +187,9 @@ class PromptClient { // (structural | localized tweak | minor edit | new | ""), so an agent can // auto-reload a tweak but hold a structural change. Needs the optional // `@nats-io/transport-node` dependency and natsUrl set. Returns the connection. - async subscribe(uri, onChange) { + // A throw or rejection from onChange goes to onError (default: console.error) + // instead of becoming an unhandled rejection that exits the host process. + async subscribe(uri, onChange, { onError = (err) => console.error(`priompt subscribe ${uri}:`, err) } = {}) { if (!this._natsUrl) throw new Error("set natsUrl on PromptClient to subscribe"); const { connect } = require("@nats-io/transport-node"); await checkNatsAuth(this._natsUrl, this._natsToken); @@ -199,12 +201,15 @@ class PromptClient { let ev; try { ev = JSON.parse(m.string()); - } catch { - ev = { version: m.string() }; // pre-0.7 bare-hash body - } - onChange(ev.version || "", ev.classification || ""); + } catch {} + // pre-0.7 bare-hash body, or anything that isn't an event object + // (an all-digit hash parses as a number, "null" as null). + if (!ev || typeof ev !== "object") ev = { version: m.string() }; + Promise.resolve() + .then(() => onChange(String(ev.version || ""), ev.classification || "")) + .catch(onError); } - })(); + })().catch(onError); return nc; } diff --git a/test/subscribe.test.js b/test/subscribe.test.js index f02869c..d653550 100644 --- a/test/subscribe.test.js +++ b/test/subscribe.test.js @@ -26,3 +26,35 @@ test("subscribe delivers version and classification", async (t) => { }); assert.deepStrictEqual(await got, { version: "abc123", classification: "minor edit" }); }); + +test("a throwing onChange or a malformed body does not crash the process", async (t) => { + const broker = await startFakeNats({ token: "s3cret" }); + t.after(() => broker.close()); + const client = new PromptClient({ host: "localhost:1", natsUrl: broker.url.replace("nats://", "nats://s3cret@") }); + t.after(() => client.close()); + + const errors = []; + const seen = []; + const done = new Promise((resolve) => { + const onChange = async (version) => { + seen.push(version); + if (seen.length === 1) throw new Error("sync-ish handler bug"); + if (seen.length === 2) return Promise.reject(new Error("async handler bug")); + if (seen.length === 4) resolve(); + }; + client.subscribe("priompt://acme/a", onChange, { onError: (err) => errors.push(err.message) }) + .then((nc) => { + t.after(() => nc.close()); + return nc.flush(); + }) + .then(() => { + broker.publish("priompt.acme.a", JSON.stringify({ version: "v1" })); + broker.publish("priompt.acme.a", JSON.stringify({ version: "v2" })); + broker.publish("priompt.acme.a", "null"); + broker.publish("priompt.acme.a", "12345678"); + }); + }); + await done; + assert.deepStrictEqual(seen, ["v1", "v2", "null", "12345678"]); + assert.deepStrictEqual(errors, ["sync-ish handler bug", "async handler bug"]); +});