From: Jérôme Benoit Date: Thu, 22 Feb 2024 09:34:11 +0000 (+0100) Subject: refactor: improve error reporting in the UI server code X-Git-Tag: v1.2.38~61 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=776cdee38610a544b49add6a5fa96010faff5c4f;p=e-mobility-charging-stations-simulator.git refactor: improve error reporting in the UI server code Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ui-server/AbstractUIServer.ts b/src/charging-station/ui-server/AbstractUIServer.ts index 61593db5..566d4961 100644 --- a/src/charging-station/ui-server/AbstractUIServer.ts +++ b/src/charging-station/ui-server/AbstractUIServer.ts @@ -19,6 +19,9 @@ import { type ResponsePayload, type UIServerConfiguration } from '../../types/index.js' +import { logger } from '../../utils/index.js' + +const moduleName = 'AbstractUIServer' export abstract class AbstractUIServer { public readonly chargingStations: Map @@ -84,6 +87,12 @@ export abstract class AbstractUIServer { } protected startHttpServer (): void { + this.httpServer.on('error', error => { + logger.error( + `${this.logPrefix(moduleName, 'start.httpServer.on.error')} HTTP server error:`, + error + ) + }) if (!this.httpServer.listening) { this.httpServer.listen(this.uiServerConfiguration.options) } @@ -116,6 +125,7 @@ export abstract class AbstractUIServer { private stopHttpServer (): void { if (this.httpServer.listening) { this.httpServer.close() + this.httpServer.removeAllListeners() } } diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index 368a09fe..1e1a372c 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -91,6 +91,16 @@ export class UIWebSocketServer extends AbstractUIServer { } }) this.httpServer.on('upgrade', (req: IncomingMessage, socket: Duplex, head: Buffer): void => { + const onSocketError = (error: Error): void => { + logger.error( + `${this.logPrefix( + moduleName, + 'start.httpServer.on.upgrade' + )} Socket error at connection upgrade event handling:`, + error + ) + } + socket.on('error', onSocketError) this.authenticate(req, err => { if (err != null) { socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`) @@ -106,11 +116,12 @@ export class UIWebSocketServer extends AbstractUIServer { `${this.logPrefix( moduleName, 'start.httpServer.on.upgrade' - )} Error at handling connection upgrade:`, + )} Error at connection upgrade event handling:`, error ) } }) + socket.removeListener('error', onSocketError) }) this.startHttpServer() }