Implement error handling and propagation in IPC and UI server code
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
index c67da00e1f6c8643e0f35032ff759d6bfe0cb0f0..5f02f2ea47f9de93276970d53672e078a7fbc9b8 100644 (file)
@@ -1,13 +1,16 @@
-import { Protocol, ProtocolVersion } from '../../types/UIProtocol';
-
-import { AbstractUIServer } from './AbstractUIServer';
-import Configuration from '../../utils/Configuration';
 import { IncomingMessage } from 'http';
-import { ServerOptions } from '../../types/ConfigurationData';
-import UIServiceFactory from './ui-services/UIServiceFactory';
-import Utils from '../../utils/Utils';
+
 import WebSocket from 'ws';
+
+import { ServerOptions } from '../../types/ConfigurationData';
+import { Protocol, ProtocolVersion } from '../../types/UIProtocol';
+import Configuration from '../../utils/Configuration';
 import logger from '../../utils/Logger';
+import Utils from '../../utils/Utils';
+import { AbstractUIServer } from './AbstractUIServer';
+import UIServiceFactory from './ui-services/UIServiceFactory';
+
+const moduleName = 'UIWebSocketServer';
 
 export default class UIWebSocketServer extends AbstractUIServer {
   public constructor(options?: ServerOptions) {
@@ -25,16 +28,19 @@ export default class UIWebSocketServer extends AbstractUIServer {
         this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
       }
       // FIXME: check connection validity
-      socket.on('message', (messageData) => {
+      socket.on('message', (rawData) => {
         this.uiServices
           .get(version)
-          .messageHandler(messageData)
+          .requestHandler(rawData)
           .catch(() => {
-            logger.error(`${this.logPrefix()} Error while handling message data: %j`, messageData);
+            /* Error caught by AbstractUIService */
           });
       });
       socket.on('error', (error) => {
-        logger.error(`${this.logPrefix()} Error on WebSocket: %j`, error);
+        logger.error(
+          `${this.logPrefix(moduleName, 'start.socket.onerror')} Error on WebSocket:`,
+          error
+        );
       });
     });
   }
@@ -43,12 +49,20 @@ export default class UIWebSocketServer extends AbstractUIServer {
     this.server.close();
   }
 
-  public sendResponse(message: string): void {
-    this.broadcastToClients(message);
+  public sendRequest(request: string): void {
+    this.broadcastToClients(request);
+  }
+
+  public sendResponse(response: string): void {
+    this.broadcastToClients(response);
   }
 
-  public logPrefix(): string {
-    return Utils.logPrefix(' UI WebSocket Server:');
+  public logPrefix(modName?: string, methodName?: string): string {
+    const logMsg =
+      modName && methodName
+        ? ` UI WebSocket Server | ${modName}.${methodName}:`
+        : ' UI WebSocket Server |';
+    return Utils.logPrefix(logMsg);
   }
 
   private broadcastToClients(message: string): void {