PluginManager: catch errors during plugin flux handlers

This commit is contained in:
Vendicated 2024-06-21 22:42:25 +02:00
parent b9392c3be2
commit 0b033aa51b
No known key found for this signature in database
GPG key ID: D66986BAF75ECF18
3 changed files with 103 additions and 97 deletions

View file

@ -169,7 +169,18 @@ export function subscribePluginFluxEvents(p: Plugin, fluxDispatcher: typeof Flux
logger.debug("Subscribing to flux events of plugin", p.name);
for (const [event, handler] of Object.entries(p.flux)) {
fluxDispatcher.subscribe(event as FluxEvents, handler);
const wrappedHandler = p.flux[event] = function () {
try {
const res = handler.apply(p, arguments as any);
return res instanceof Promise
? res.catch(e => logger.error(`${p.name}: Error while handling ${event}\n`, e))
: res;
} catch (e) {
logger.error(`${p.name}: Error while handling ${event}\n`, e);
}
};
fluxDispatcher.subscribe(event as FluxEvents, wrappedHandler);
}
}
}