From 486791f53ea0bfa22b3aa81572da8ac8830c7f5f Mon Sep 17 00:00:00 2001 From: James Murdza Date: Fri, 25 Oct 2024 07:06:07 -0600 Subject: [PATCH] refactor: simplify error handling --- backend/server/src/index.ts | 20 ++++++++++---------- backend/server/src/utils.ts | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/backend/server/src/index.ts b/backend/server/src/index.ts index d87c71b..83f06bc 100644 --- a/backend/server/src/index.ts +++ b/backend/server/src/index.ts @@ -11,18 +11,22 @@ import { SandboxManager } from "./SandboxManager" import { SecureGitClient } from "./SecureGitClient" import { socketAuth } from "./socketAuth"; // Import the new socketAuth middleware +// Log errors and send a notification to the client +export const handleErrors = (message: string, error: any, socket: any) => { + console.error(message, error); + socket.emit("error", `${message} ${error.message ?? error}`); +}; + // Handle uncaught exceptions process.on("uncaughtException", (error) => { console.error("Uncaught Exception:", error) // Do not exit the process - // You can add additional logging or recovery logic here }) // Handle unhandled promise rejections process.on("unhandledRejection", (reason, promise) => { console.error("Unhandled Rejection at:", promise, "reason:", reason) // Do not exit the process - // You can also handle the rejected promise here if needed }) // Check if the sandbox owner is connected @@ -122,8 +126,7 @@ io.on("connection", async (socket) => { const response = await handler(options) callback?.(response); } catch (e: any) { - console.error(`Error processing event "${event}":`, e); - socket.emit("error", `Error: ${event}. ${e.message ?? e}`); + handleErrors(`Error processing event "${event}":`, e, socket); } }); }); @@ -143,19 +146,16 @@ io.on("connection", async (socket) => { ) } } catch (e: any) { - console.log("Error disconnecting:", e) - socket.emit("error", `Error: disconnecting. ${e.message ?? e}`) + handleErrors("Error disconnecting:", e, socket); } }) } catch (e: any) { - console.error(`Error initializing sandbox ${data.sandboxId}:`, e); - socket.emit("error", `Error: initialize sandbox ${data.sandboxId}. ${e.message ?? e}`); + handleErrors(`Error initializing sandbox ${data.sandboxId}:`, e, socket); } } catch (e: any) { - console.error("Error connecting:", e) - socket.emit("error", `Error: connection. ${e.message ?? e}`) + handleErrors("Error connecting:", e, socket); } }) diff --git a/backend/server/src/utils.ts b/backend/server/src/utils.ts index 5ae1377..dd33984 100644 --- a/backend/server/src/utils.ts +++ b/backend/server/src/utils.ts @@ -20,4 +20,4 @@ export class LockManager { } return await this.locks[key] } -} +} \ No newline at end of file