Simplify error handling for IPC handlers (#119)

This commit is contained in:
Will Chen
2025-05-08 23:47:05 -07:00
committed by GitHub
parent c203b1d009
commit b2eb05a1bc
2 changed files with 138 additions and 135 deletions

View File

@@ -0,0 +1,24 @@
import { ipcMain, IpcMainInvokeEvent } from "electron";
import log from "electron-log";
export function createSafeHandler(logger: log.LogFunctions) {
return (
channel: string,
fn: (event: IpcMainInvokeEvent, ...args: any[]) => Promise<any>,
) => {
ipcMain.handle(
channel,
async (event: IpcMainInvokeEvent, ...args: any[]) => {
try {
return await fn(event, ...args);
} catch (error) {
logger.error(
`Error in ${fn.name}: args: ${JSON.stringify(args)}`,
error,
);
throw new Error(`[${channel}] ${error}`);
}
},
);
};
}