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}`);
}
},
);
};
}

View File

@@ -1,4 +1,3 @@
import { ipcMain } from "electron";
import { db } from "../../db";
import { apps, messages } from "../../db/schema";
import { desc, eq, and, gt } from "drizzle-orm";
@@ -8,15 +7,17 @@ import path from "node:path";
import { getDyadAppPath } from "../../paths/paths";
import git from "isomorphic-git";
import { promises as fsPromises } from "node:fs";
// Import our utility modules
import { withLock } from "../utils/lock_utils";
import { getGitAuthor } from "../utils/git_author";
import log from "electron-log";
import { createSafeHandler } from "./safe_handle";
const logger = log.scope("version_handlers");
const handle = createSafeHandler(logger);
export function registerVersionHandlers() {
ipcMain.handle("list-versions", async (_, { appId }: { appId: number }) => {
handle("list-versions", async (_, { appId }: { appId: number }) => {
const app = await db.query.apps.findFirst({
where: eq(apps.id, appId),
});
@@ -32,7 +33,6 @@ export function registerVersionHandlers() {
return [];
}
try {
const commits = await git.log({
fs,
dir: appPath,
@@ -45,13 +45,9 @@ export function registerVersionHandlers() {
message: commit.commit.message,
timestamp: commit.commit.author.timestamp,
})) satisfies Version[];
} catch (error: any) {
logger.error(`Error listing versions for app ${appId}:`, error);
throw new Error(`Failed to list versions: ${error.message}`);
}
});
ipcMain.handle(
handle(
"get-current-branch",
async (_, { appId }: { appId: number }): Promise<BranchResult> => {
const app = await db.query.apps.findFirst({
@@ -86,7 +82,7 @@ export function registerVersionHandlers() {
},
);
ipcMain.handle(
handle(
"revert-version",
async (
_,
@@ -106,7 +102,6 @@ export function registerVersionHandlers() {
const appPath = getDyadAppPath(app.path);
try {
await git.checkout({
fs,
dir: appPath,
@@ -205,18 +200,11 @@ export function registerVersionHandlers() {
);
}
}
} catch (error: any) {
logger.error(
`Error reverting to version ${previousVersionId} for app ${appId}:`,
error,
);
throw new Error(`Failed to revert version: ${error.message}`);
}
});
},
);
ipcMain.handle(
handle(
"checkout-version",
async (
_,
@@ -233,21 +221,12 @@ export function registerVersionHandlers() {
const appPath = getDyadAppPath(app.path);
try {
// Checkout the target commit
await git.checkout({
fs,
dir: appPath,
ref: versionId,
force: true,
});
} catch (error: any) {
logger.error(
`Error checking out version ${versionId} for app ${appId}:`,
error,
);
throw new Error(`Failed to checkout version: ${error.message}`);
}
});
},
);