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); logger.debug("Subscribing to flux events of plugin", p.name);
for (const [event, handler] of Object.entries(p.flux)) { 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);
} }
} }
} }

View file

@ -154,8 +154,6 @@ export default definePlugin({
} }
}, },
MESSAGE_CREATE({ message, optimistic }: { message: Message; optimistic: boolean; }) { MESSAGE_CREATE({ message, optimistic }: { message: Message; optimistic: boolean; }) {
// Apparently without this try/catch, discord's socket connection dies if any part of this errors
try {
if (optimistic) return; if (optimistic) return;
const channel = ChannelStore.getChannel(message.channel_id); const channel = ChannelStore.getChannel(message.channel_id);
if (!shouldNotify(message, message.channel_id)) return; if (!shouldNotify(message, message.channel_id)) return;
@ -249,9 +247,6 @@ export default definePlugin({
if (shouldIgnoreForChannelType(channel)) return; if (shouldIgnoreForChannelType(channel)) return;
sendMsgNotif(titleString, finalMsg, message); sendMsgNotif(titleString, finalMsg, message);
} catch (err) {
XSLog.error(`Failed to catch MESSAGE_CREATE: ${err}`);
}
} }
} }
}); });

View file

@ -128,7 +128,7 @@ export interface PluginDef {
* Allows you to subscribe to Flux events * Allows you to subscribe to Flux events
*/ */
flux?: { flux?: {
[E in FluxEvents]?: (event: any) => void; [E in FluxEvents]?: (event: any) => void | Promise<void>;
}; };
/** /**
* Allows you to manipulate context menus * Allows you to manipulate context menus