Simplify handlers & IPC client: move from Result pattern to throwing errors (#120)

This commit is contained in:
Will Chen
2025-05-09 15:14:12 -07:00
committed by GitHub
parent 26305ee090
commit c71638a508
25 changed files with 618 additions and 990 deletions

View File

@@ -1,7 +1,7 @@
import { ipcMain, IpcMainInvokeEvent } from "electron";
import log from "electron-log";
export function createSafeHandler(logger: log.LogFunctions) {
export function createLoggedHandler(logger: log.LogFunctions) {
return (
channel: string,
fn: (event: IpcMainInvokeEvent, ...args: any[]) => Promise<any>,
@@ -9,8 +9,11 @@ export function createSafeHandler(logger: log.LogFunctions) {
ipcMain.handle(
channel,
async (event: IpcMainInvokeEvent, ...args: any[]) => {
logger.log(`IPC: ${channel} called with args: ${JSON.stringify(args)}`);
try {
return await fn(event, ...args);
const result = await fn(event, ...args);
logger.log(`IPC: ${channel} returned: ${JSON.stringify(result)}`);
return result;
} catch (error) {
logger.error(
`Error in ${fn.name}: args: ${JSON.stringify(args)}`,