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,9 +1,11 @@
import { ipcMain } from "electron";
import log from "electron-log";
import fetch from "node-fetch";
import { createLoggedHandler } from "./safe_handle";
const logger = log.scope("upload_handlers");
const handle = createLoggedHandler(logger);
interface UploadToSignedUrlParams {
url: string;
contentType: string;
@@ -11,49 +13,37 @@ interface UploadToSignedUrlParams {
}
export function registerUploadHandlers() {
ipcMain.handle(
"upload-to-signed-url",
async (_, params: UploadToSignedUrlParams) => {
const { url, contentType, data } = params;
logger.debug("IPC: upload-to-signed-url called");
handle("upload-to-signed-url", async (_, params: UploadToSignedUrlParams) => {
const { url, contentType, data } = params;
logger.debug("IPC: upload-to-signed-url called");
try {
// Validate the signed URL
if (!url || typeof url !== "string" || !url.startsWith("https://")) {
throw new Error("Invalid signed URL provided");
}
// Validate the signed URL
if (!url || typeof url !== "string" || !url.startsWith("https://")) {
throw new Error("Invalid signed URL provided");
}
// Validate content type
if (!contentType || typeof contentType !== "string") {
throw new Error("Invalid content type provided");
}
// Validate content type
if (!contentType || typeof contentType !== "string") {
throw new Error("Invalid content type provided");
}
// Perform the upload to the signed URL
const response = await fetch(url, {
method: "PUT",
headers: {
"Content-Type": contentType,
},
body: JSON.stringify(data),
});
// Perform the upload to the signed URL
const response = await fetch(url, {
method: "PUT",
headers: {
"Content-Type": contentType,
},
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error(
`Upload failed with status ${response.status}: ${response.statusText}`,
);
}
if (!response.ok) {
throw new Error(
`Upload failed with status ${response.status}: ${response.statusText}`,
);
}
logger.debug("Successfully uploaded data to signed URL");
return { success: true };
} catch (error) {
logger.error("Failed to upload to signed URL:", error);
return {
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
},
);
logger.debug("Successfully uploaded data to signed URL");
});
logger.debug("Registered upload IPC handlers");
}