Skip to content

Commit

Permalink
fix(core/pubsubhub): log errors from event handlers (#4833)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi authored Nov 26, 2024
1 parent 1f06b11 commit fae88d4
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/core/pubsubhub.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
export const name = "core/pubsubhub";

import { expose } from "./expose-modules.js";
import { showError } from "./utils.js";

const subscriptions = new EventTarget();

Expand Down Expand Up @@ -36,7 +37,15 @@ export function pub(topic, detail) {
* used for unsubscribing from messages.
*/
export function sub(topic, cb, options = { once: false }) {
const listener = e => cb(e.detail);
/** @param {CustomEvent} ev */
const listener = async ev => {
try {
await cb(ev.detail);
} catch (error) {
const msg = `Error in handler for topic "${topic}": ${error.message}`;
showError(msg, `sub:${topic}`, { cause: error });
}
};
subscriptions.addEventListener(topic, listener, options);
}

Expand Down

0 comments on commit fae88d4

Please sign in to comment.