fix: only send UI server response when needed
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIHttpServer.ts
index 186669ef83e96d42c2c2fed420f2153ba4a1bfff..a831446b9c5f93d0e8bd3cd55543dc79957b88bc 100644 (file)
@@ -2,6 +2,8 @@ import type { IncomingMessage, RequestListener, ServerResponse } from 'node:http
 
 import { StatusCodes } from 'http-status-codes';
 
+import { AbstractUIServer } from './AbstractUIServer';
+import { UIServerUtils } from './UIServerUtils';
 import { BaseError } from '../../exception';
 import {
   type ProcedureName,
@@ -13,8 +15,7 @@ import {
   ResponseStatus,
   type UIServerConfiguration,
 } from '../../types';
-import { Utils, logger } from '../../utils';
-import { AbstractUIServer, UIServerUtils } from '../internal';
+import { Constants, Utils, logger } from '../../utils';
 
 const moduleName = 'UIHttpServer';
 
@@ -117,10 +118,19 @@ export class UIHttpServer extends AbstractUIServer {
             const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload;
             this.uiServices
               .get(version)
-              ?.requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {}))
-              .catch(() => {
-                /* Error caught by AbstractUIService */
-              });
+              ?.requestHandler(
+                this.buildProtocolRequest(
+                  uuid,
+                  procedureName,
+                  body ?? Constants.EMPTY_FREEZED_OBJECT
+                )
+              )
+              .then((protocolResponse: ProtocolResponse) => {
+                if (Utils.isNullOrUndefined(protocolResponse)) {
+                  this.sendResponse(protocolResponse);
+                }
+              })
+              .catch(Constants.EMPTY_FUNCTION);
           });
       } else {
         throw new BaseError(`Unsupported HTTP method: '${req.method}'`);