refactor: simplify server error handling

This commit is contained in:
James Murdza 2024-10-25 07:06:07 -06:00
parent aa554fa39d
commit 87a74d40d6
2 changed files with 11 additions and 11 deletions

View File

@ -11,18 +11,22 @@ import { SandboxManager } from "./SandboxManager"
import { SecureGitClient } from "./SecureGitClient" import { SecureGitClient } from "./SecureGitClient"
import { socketAuth } from "./socketAuth"; // Import the new socketAuth middleware 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 // Handle uncaught exceptions
process.on("uncaughtException", (error) => { process.on("uncaughtException", (error) => {
console.error("Uncaught Exception:", error) console.error("Uncaught Exception:", error)
// Do not exit the process // Do not exit the process
// You can add additional logging or recovery logic here
}) })
// Handle unhandled promise rejections // Handle unhandled promise rejections
process.on("unhandledRejection", (reason, promise) => { process.on("unhandledRejection", (reason, promise) => {
console.error("Unhandled Rejection at:", promise, "reason:", reason) console.error("Unhandled Rejection at:", promise, "reason:", reason)
// Do not exit the process // Do not exit the process
// You can also handle the rejected promise here if needed
}) })
// Check if the sandbox owner is connected // Check if the sandbox owner is connected
@ -122,8 +126,7 @@ io.on("connection", async (socket) => {
const response = await handler(options) const response = await handler(options)
callback?.(response); callback?.(response);
} catch (e: any) { } catch (e: any) {
console.error(`Error processing event "${event}":`, e); handleErrors(`Error processing event "${event}":`, e, socket);
socket.emit("error", `Error: ${event}. ${e.message ?? e}`);
} }
}); });
}); });
@ -143,19 +146,16 @@ io.on("connection", async (socket) => {
) )
} }
} catch (e: any) { } catch (e: any) {
console.log("Error disconnecting:", e) handleErrors("Error disconnecting:", e, socket);
socket.emit("error", `Error: disconnecting. ${e.message ?? e}`)
} }
}) })
} catch (e: any) { } catch (e: any) {
console.error(`Error initializing sandbox ${data.sandboxId}:`, e); handleErrors(`Error initializing sandbox ${data.sandboxId}:`, e, socket);
socket.emit("error", `Error: initialize sandbox ${data.sandboxId}. ${e.message ?? e}`);
} }
} catch (e: any) { } catch (e: any) {
console.error("Error connecting:", e) handleErrors("Error connecting:", e, socket);
socket.emit("error", `Error: connection. ${e.message ?? e}`)
} }
}) })

View File

@ -20,4 +20,4 @@ export class LockManager {
} }
return await this.locks[key] return await this.locks[key]
} }
} }