From 2eb2388e128ec60a94fe8516ba9e829c82289e99 Mon Sep 17 00:00:00 2001 From: James Murdza Date: Fri, 25 Oct 2024 06:40:47 -0600 Subject: [PATCH] refactor: restructure error handling --- backend/server/src/index.ts | 76 +++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/backend/server/src/index.ts b/backend/server/src/index.ts index 54f1315..d87c71b 100644 --- a/backend/server/src/index.ts +++ b/backend/server/src/index.ts @@ -107,50 +107,52 @@ io.on("connection", async (socket) => { } } - const sandboxManager = sandboxManagers[data.sandboxId] ?? new SandboxManager( - data.sandboxId, - data.userId, - { aiWorker, dokkuClient, gitClient, socket } - ) - try { + const sandboxManager = sandboxManagers[data.sandboxId] ?? new SandboxManager( + data.sandboxId, + data.userId, + { aiWorker, dokkuClient, gitClient, socket } + ) + sandboxManager.initializeContainer() + + Object.entries(sandboxManager.handlers()).forEach(([event, handler]) => { + socket.on(event, async (options: any, callback?: (response: any) => void) => { + try { + 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}`); + } + }); + }); + + socket.on("disconnect", async () => { + try { + if (data.isOwner) { + connections[data.sandboxId]-- + } + + await sandboxManager.disconnect() + + if (data.isOwner && connections[data.sandboxId] <= 0) { + socket.broadcast.emit( + "disableAccess", + "The sandbox owner has disconnected." + ) + } + } catch (e: any) { + console.log("Error disconnecting:", e) + socket.emit("error", `Error: disconnecting. ${e.message ?? e}`) + } + }) + } catch (e: any) { console.error(`Error initializing sandbox ${data.sandboxId}:`, e); socket.emit("error", `Error: initialize sandbox ${data.sandboxId}. ${e.message ?? e}`); } - Object.entries(sandboxManager.handlers()).forEach(([event, handler]) => { - socket.on(event, async (options: any, callback?: (response: any) => void) => { - try { - 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}`); - } - }); - }); - - socket.on("disconnect", async () => { - try { - if (data.isOwner) { - connections[data.sandboxId]-- - } - - await sandboxManager.disconnect() - - if (data.isOwner && connections[data.sandboxId] <= 0) { - socket.broadcast.emit( - "disableAccess", - "The sandbox owner has disconnected." - ) - } - } catch (e: any) { - console.log("Error disconnecting:", e) - socket.emit("error", `Error: disconnecting. ${e.message ?? e}`) - } - }) } catch (e: any) { console.error("Error connecting:", e) socket.emit("error", `Error: connection. ${e.message ?? e}`)