From 7375968c99fc22707e16e5e7923ca130c824ce5b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 20 Nov 2023 21:23:41 +0100 Subject: [PATCH] refactor: improve OCPP error defaults and usage MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ChargingStationWorkerBroadcastChannel.ts | 4 ++-- .../ocpp/OCPPRequestService.ts | 20 ++++++++----------- .../ui-services/AbstractUIService.ts | 4 ++-- src/exception/BaseError.ts | 4 +++- src/exception/OCPPError.ts | 7 ++++--- 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index 5188d974..a8d9b045 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -293,8 +293,8 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne command, requestPayload, commandResponse: commandResponse as CommandResponse, - errorMessage: (error as Error).message, - errorStack: (error as Error).stack, + errorMessage: (error as OCPPError).message, + errorStack: (error as OCPPError).stack, errorDetails: (error as OCPPError).details, }; } finally { diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 77e87f43..849ecbd0 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -23,13 +23,7 @@ import { type ResponseCallback, type ResponseType, } from '../../types'; -import { - Constants, - cloneObject, - handleSendMessageError, - isNullOrUndefined, - logger, -} from '../../utils'; +import { cloneObject, handleSendMessageError, isNullOrUndefined, logger } from '../../utils'; const moduleName = 'OCPPRequestService'; @@ -421,7 +415,7 @@ export abstract class OCPPRequestService { ErrorType.GENERIC_ERROR, `Timeout for message id '${messageId}'`, commandName, - (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT, + (messagePayload as OCPPError).details, ), ); }, OCPPConstants.OCPP_WEBSOCKET_TIMEOUT); @@ -468,7 +462,7 @@ export abstract class OCPPRequestService { params?.skipBufferingOnError === false ? '' : 'non ' }buffered message id '${messageId}' with content '${messageToSend}'`, commandName, - (messagePayload as JsonObject)?.details ?? Constants.EMPTY_FROZEN_OBJECT, + (messagePayload as JsonObject).details, ), ); } @@ -518,9 +512,11 @@ export abstract class OCPPRequestService { messageToSend = JSON.stringify([ messageType, messageId, - (messagePayload as OCPPError)?.code ?? ErrorType.GENERIC_ERROR, - (messagePayload as OCPPError)?.message ?? '', - (messagePayload as OCPPError)?.details ?? { commandName }, + (messagePayload as OCPPError).code, + (messagePayload as OCPPError).message, + (messagePayload as OCPPError).details ?? { + command: (messagePayload as OCPPError).command ?? commandName, + }, ] as ErrorResponse); break; } diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 20531c6f..a190b603 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -105,8 +105,8 @@ export abstract class AbstractUIService { command, requestPayload, responsePayload, - errorMessage: (error as Error).message, - errorStack: (error as Error).stack, + errorMessage: (error as OCPPError).message, + errorStack: (error as OCPPError).stack, errorDetails: (error as OCPPError).details, }; } diff --git a/src/exception/BaseError.ts b/src/exception/BaseError.ts index 84421eac..2163f6ee 100644 --- a/src/exception/BaseError.ts +++ b/src/exception/BaseError.ts @@ -3,7 +3,9 @@ export class BaseError extends Error { super(message); this.name = new.target.name; Object.setPrototypeOf(this, new.target.prototype); - Error.captureStackTrace ? Error.captureStackTrace(this, this.constructor) : this.createStack(); + typeof Error.captureStackTrace === 'function' + ? Error.captureStackTrace(this, this.constructor) + : this.createStack(); } private createStack(): void { diff --git a/src/exception/OCPPError.ts b/src/exception/OCPPError.ts index f624d7e7..5736ad27 100644 --- a/src/exception/OCPPError.ts +++ b/src/exception/OCPPError.ts @@ -22,8 +22,9 @@ export class OCPPError extends BaseError { ) { super(message); - this.code = code ?? ErrorType.GENERIC_ERROR; - this.command = command; - this.details = details ?? Constants.EMPTY_FROZEN_OBJECT; + this.code = code; + this.command = + command ?? (Constants.UNKNOWN_COMMAND as RequestCommand | IncomingRequestCommand); + this.details = details; } } -- 2.34.1