UI server: logging and code refinements
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 27 Aug 2022 09:26:40 +0000 (11:26 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 27 Aug 2022 09:26:40 +0000 (11:26 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
e-mobility-charging-stations-simulator.code-workspace [new file with mode: 0644]
src/charging-station/ui-server/UIHttpServer.ts
src/ui/web/src/composable/UIClient.ts

diff --git a/e-mobility-charging-stations-simulator.code-workspace b/e-mobility-charging-stations-simulator.code-workspace
new file mode 100644 (file)
index 0000000..e0dbb0c
--- /dev/null
@@ -0,0 +1,11 @@
+{
+       "folders": [
+               {
+                       "path": "."
+               },
+               {
+                       "path": "src/ui/web"
+               }
+       ],
+       "settings": {}
+}
\ No newline at end of file
index 2d729bfc3be22b57ca4a0e71e26702245e5fedd2..a9b620635d03df20db1898e6894fb94be00c8993 100644 (file)
@@ -7,6 +7,7 @@ import { ServerOptions } from '../../types/ConfigurationData';
 import {
   ProcedureName,
   Protocol,
+  ProtocolRequest,
   ProtocolResponse,
   ProtocolVersion,
   RequestPayload,
@@ -57,7 +58,7 @@ export default class UIHttpServer extends AbstractUIServer {
       this.responseHandlers.delete(uuid);
     } else {
       logger.error(
-        `${this.logPrefix()} ${moduleName}.sendResponse: Response received for unknown request: ${response}`
+        `${this.logPrefix()} ${moduleName}.sendResponse: Response for unknown request: ${response}`
       );
     }
   }
@@ -83,10 +84,7 @@ export default class UIHttpServer extends AbstractUIServer {
       }
       req.on('error', (error) => {
         logger.error(
-          `${this.logPrefix(
-            moduleName,
-            'requestListener.req.onerror'
-          )} Error at incoming request handling:`,
+          `${this.logPrefix(moduleName, 'requestListener.req.onerror')} Error on HTTP request:`,
           error
         );
       });
@@ -95,13 +93,12 @@ export default class UIHttpServer extends AbstractUIServer {
       }
       if (req.method === 'POST') {
         const bodyBuffer = [];
-        let body: RequestPayload;
         req
           .on('data', (chunk) => {
             bodyBuffer.push(chunk);
           })
           .on('end', () => {
-            body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload;
+            const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload;
             this.uiServices
               .get(version)
               .requestHandler(this.buildRequest(uuid, procedureName, body ?? {}))
@@ -113,6 +110,10 @@ export default class UIHttpServer extends AbstractUIServer {
         throw new BaseError(`Unsupported HTTP method: '${req.method}'`);
       }
     } catch (error) {
+      logger.error(
+        `${this.logPrefix(moduleName, 'requestListener')} Handle HTTP request error:`,
+        error
+      );
       this.sendResponse(this.buildResponse(uuid, { status: ResponseStatus.FAILURE }));
     }
   }
@@ -122,11 +123,11 @@ export default class UIHttpServer extends AbstractUIServer {
     procedureName: ProcedureName,
     requestPayload: RequestPayload
   ): string {
-    return JSON.stringify([id, procedureName, requestPayload]);
+    return JSON.stringify([id, procedureName, requestPayload] as ProtocolRequest);
   }
 
   private buildResponse(id: string, responsePayload: ResponsePayload): string {
-    return JSON.stringify([id, responsePayload]);
+    return JSON.stringify([id, responsePayload] as ProtocolResponse);
   }
 
   private responseStatusToStatusCode(status: ResponseStatus): StatusCodes {
index f699a837e9524fa0d981fd4e503db568f423b83d..2095b1671775eb2f60c622e3c9b4e7fb4a50697b 100644 (file)
@@ -97,6 +97,9 @@ export default class UIClient {
       config.emobility.protocol
     );
     this._ws.onmessage = this.responseHandler.bind(this);
+    this._ws.onerror = (error) => {
+      console.error('WebSocket error: ', error);
+    };
   }
 
   private setResponseHandler(
@@ -160,7 +163,7 @@ export default class UIClient {
           this.getResponseHandler(uuid)?.reject(response);
           break;
         default:
-          throw new Error(`Response status not supported: ${response.status}`);
+          console.error(`Response status not supported: ${response.status}`);
       }
       this.deleteResponseHandler(uuid);
     } else {