Simplify handlers & IPC client: move from Result pattern to throwing errors (#120)
This commit is contained in:
@@ -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)}`,
|
||||
|
||||
Reference in New Issue
Block a user