From 9295f90741a43ba3e527b42472716dc3226bd3eb Mon Sep 17 00:00:00 2001 From: creations Date: Thu, 27 Feb 2025 13:32:51 -0500 Subject: [PATCH] move errors to fit in logger.error --- src/helpers/logger.ts | 6 +++++- src/index.ts | 3 +-- src/server.ts | 12 ++++++++---- src/webSocketHandler.ts | 4 ++-- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/helpers/logger.ts b/src/helpers/logger.ts index 45ad968..331be1d 100644 --- a/src/helpers/logger.ts +++ b/src/helpers/logger.ts @@ -130,7 +130,7 @@ class Logger { } public error( - message: string | string[] | Error | Error[], + message: string | Error | (string | Error)[], breakLine: boolean = false, ): void { const stack: string = new Error().stack || ""; @@ -183,6 +183,10 @@ class Logger { this.writeConsoleMessageColored(logMessageParts, breakLine); } + public space(): void { + console.log(); + } + private writeConsoleMessageColored( logMessageParts: ILogMessageParts, breakLine: boolean = false, diff --git a/src/index.ts b/src/index.ts index 7cd8048..6d2801d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,6 @@ async function main(): Promise { } main().catch((error: Error) => { - logger.error("Error initializing the server:"); - logger.error(error as Error); + logger.error(["Error initializing the server:", error]); process.exit(1); }); diff --git a/src/server.ts b/src/server.ts index 7b0988b..7cae1f8 100644 --- a/src/server.ts +++ b/src/server.ts @@ -83,8 +83,10 @@ class ServerHandler { return new Response("Not Found", { status: 404 }); } } catch (error) { - logger.error(`Error serving static file: ${pathname}`); - logger.error(error as Error); + logger.error([ + `Error serving static file: ${pathname}`, + error as Error, + ]); return new Response("Internal Server Error", { status: 500 }); } } @@ -179,8 +181,10 @@ class ServerHandler { } } } catch (error: unknown) { - logger.error(`Error handling route ${request.url}:`); - logger.error(error as Error); + logger.error([ + `Error handling route ${request.url}:`, + error as Error, + ]); response = Response.json( { diff --git a/src/webSocketHandler.ts b/src/webSocketHandler.ts index 48c37b5..ce87fe8 100644 --- a/src/webSocketHandler.ts +++ b/src/webSocketHandler.ts @@ -7,7 +7,7 @@ class WebSocketHandler { try { ws.send(`You said: ${message}`); } catch (error) { - logger.error(`WebSocket send error: ${(error as Error).message}`); + logger.error(["WebSocket send error", error as Error]); } } @@ -16,7 +16,7 @@ class WebSocketHandler { try { ws.send("Welcome to the WebSocket server!"); } catch (error) { - logger.error(`WebSocket send error: ${(error as Error).message}`); + logger.error(["WebSocket send error", error as Error]); } }