Update src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index 3eb85e3089a232f21b50b5082e644dceec573505..d2122a65ad2f281c7a9de9cd6b9afcbbeeb87a9d 100644 (file)
@@ -1,16 +1,16 @@
 import Ajv, { type JSONSchemaType } from 'ajv';
 import ajvFormats from 'ajv-formats';
 
-import { OCPPConstants, type OCPPResponseService, OCPPServiceUtils } from './internal';
+import { OCPPConstants } from './OCPPConstants';
+import type { OCPPResponseService } from './OCPPResponseService';
+import { OCPPServiceUtils } from './OCPPServiceUtils';
 import type { ChargingStation } from '../../charging-station';
 import { OCPPError } from '../../exception';
 import { PerformanceStatistics } from '../../performance';
 import {
-  type EmptyObject,
   type ErrorCallback,
   type ErrorResponse,
   ErrorType,
-  type HandleErrorParams,
   type IncomingRequestCommand,
   type JsonObject,
   type JsonType,
@@ -23,7 +23,7 @@ import {
   type ResponseCallback,
   type ResponseType,
 } from '../../types';
-import { Constants, Utils, logger } from '../../utils';
+import { Constants, ErrorUtils, Utils, logger } from '../../utils';
 
 const moduleName = 'OCPPRequestService';
 
@@ -127,7 +127,7 @@ export abstract class OCPPRequestService {
         commandName
       );
     } catch (error) {
-      this.handleSendMessageError(chargingStation, commandName, error as Error, {
+      ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error, {
         throwError: true,
       });
     }
@@ -149,7 +149,7 @@ export abstract class OCPPRequestService {
         commandName
       );
     } catch (error) {
-      this.handleSendMessageError(chargingStation, commandName, error as Error);
+      ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error);
     }
   }
 
@@ -164,6 +164,10 @@ export abstract class OCPPRequestService {
       throwError: false,
     }
   ): Promise<ResponseType> {
+    params = {
+      ...{ skipBufferingOnError: false, triggerMessage: false, throwError: false },
+      ...params,
+    };
     try {
       return await this.internalSendMessage(
         chargingStation,
@@ -174,7 +178,7 @@ export abstract class OCPPRequestService {
         params
       );
     } catch (error) {
-      this.handleSendMessageError(chargingStation, commandName, error as Error, {
+      ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error, {
         throwError: params.throwError,
       });
     }
@@ -263,8 +267,13 @@ export abstract class OCPPRequestService {
     params: RequestParams = {
       skipBufferingOnError: false,
       triggerMessage: false,
+      throwError: false,
     }
   ): Promise<ResponseType> {
+    params = {
+      ...{ skipBufferingOnError: false, triggerMessage: false, throwError: false },
+      ...params,
+    };
     if (
       (chargingStation.inUnknownState() === true &&
         commandName === RequestCommand.BOOT_NOTIFICATION) ||
@@ -475,18 +484,6 @@ export abstract class OCPPRequestService {
     return messageToSend;
   }
 
-  private handleSendMessageError(
-    chargingStation: ChargingStation,
-    commandName: RequestCommand | IncomingRequestCommand,
-    error: Error,
-    params: HandleErrorParams<EmptyObject> = { throwError: false }
-  ): void {
-    logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error);
-    if (params?.throwError === true) {
-      throw error;
-    }
-  }
-
   // eslint-disable-next-line @typescript-eslint/no-unused-vars
   public abstract requestHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,