From: Jérôme Benoit Date: Mon, 18 Mar 2024 12:30:32 +0000 (+0100) Subject: refactor: improve configuration sanity checks log messages X-Git-Tag: v1.3.1~41 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ae61fa2feb89927b7d56ffdc7a54f4dff6b662b4;p=e-mobility-charging-stations-simulator.git refactor: improve configuration sanity checks log messages Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 2f758aa3..5e3771f6 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -2364,9 +2364,9 @@ export class ChargingStation extends EventEmitter { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion Configuration.getSupervisionUrlDistribution()! ) && - logger.error( + logger.warn( // eslint-disable-next-line @typescript-eslint/no-base-to-string - `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' from values '${SupervisionUrlDistribution.toString()}', defaulting to ${ + `${this.logPrefix()} Unknown supervision url distribution '${Configuration.getSupervisionUrlDistribution()}' in configuration from values '${SupervisionUrlDistribution.toString()}', defaulting to ${ SupervisionUrlDistribution.CHARGING_STATION_AFFINITY }` ) diff --git a/src/charging-station/ui-server/AbstractUIServer.ts b/src/charging-station/ui-server/AbstractUIServer.ts index b2a5b44e..9d2705f2 100644 --- a/src/charging-station/ui-server/AbstractUIServer.ts +++ b/src/charging-station/ui-server/AbstractUIServer.ts @@ -8,6 +8,7 @@ import { ApplicationProtocolVersion, AuthenticationType, type ChargingStationData, + ConfigurationSection, type ProcedureName, type ProtocolRequest, type ProtocolResponse, @@ -46,7 +47,7 @@ export abstract class AbstractUIServer { break default: throw new BaseError( - `Unsupported application protocol version ${this.uiServerConfiguration.version}` + `Unsupported application protocol version ${this.uiServerConfiguration.version} in '${ConfigurationSection.uiServer}' configuration section` ) } this.responseHandlers = new Map< diff --git a/src/charging-station/ui-server/UIServerFactory.ts b/src/charging-station/ui-server/UIServerFactory.ts index 0eb71cd4..2549a0aa 100644 --- a/src/charging-station/ui-server/UIServerFactory.ts +++ b/src/charging-station/ui-server/UIServerFactory.ts @@ -5,8 +5,10 @@ import { ApplicationProtocol, ApplicationProtocolVersion, AuthenticationType, + ConfigurationSection, type UIServerConfiguration } from '../../types/index.js' +import { logger, logPrefix } from '../../utils/index.js' import type { AbstractUIServer } from './AbstractUIServer.js' import { UIHttpServer } from './UIHttpServer.js' import { isLoopback } from './UIServerUtils.js' @@ -26,7 +28,7 @@ export class UIServerFactory { !Object.values(AuthenticationType).includes(uiServerConfiguration.authentication.type) ) { throw new BaseError( - `Unknown authentication type '${uiServerConfiguration.authentication.type}' for UI server` + `Unknown authentication type '${uiServerConfiguration.authentication.type}' in '${ConfigurationSection.uiServer}' configuration section` ) } if ( @@ -34,28 +36,26 @@ export class UIServerFactory { uiServerConfiguration.authentication?.enabled === true && uiServerConfiguration.authentication.type === AuthenticationType.PROTOCOL_BASIC_AUTH ) { - throw new BaseError('Protocol basic authentication is not supported for HTTP UI server') + throw new BaseError( + `'${uiServerConfiguration.authentication.type}' authentication type with application protocol type '${uiServerConfiguration.type}' is not supported in '${ConfigurationSection.uiServer}' configuration section` + ) } if ( uiServerConfiguration.authentication?.enabled !== true && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion !isLoopback(uiServerConfiguration.options!.host!) ) { - console.warn( - chalk.yellow( - 'Non loopback address in UI server configuration without authentication enabled. This is not recommended' - ) - ) + const logMsg = `Non loopback address in '${ConfigurationSection.uiServer}' configuration section without authentication enabled. This is not recommended` + logger.warn(`${UIServerFactory.logPrefix()} ${logMsg}`) + console.warn(chalk.yellow(logMsg)) } if ( uiServerConfiguration.type === ApplicationProtocol.WS && uiServerConfiguration.version !== ApplicationProtocolVersion.VERSION_11 ) { - console.warn( - chalk.yellow( - `Only version ${ApplicationProtocolVersion.VERSION_11} is supported for WebSocket UI server. Falling back to version ${ApplicationProtocolVersion.VERSION_11}` - ) - ) + const logMsg = `Only version ${ApplicationProtocolVersion.VERSION_11} is with application protocol type '${uiServerConfiguration.type}' supported in '${ConfigurationSection.uiServer}' configuration section. Falling back to version ${ApplicationProtocolVersion.VERSION_11}` + logger.warn(`${UIServerFactory.logPrefix()} ${logMsg}`) + console.warn(chalk.yellow(logMsg)) uiServerConfiguration.version = ApplicationProtocolVersion.VERSION_11 } switch (uiServerConfiguration.type) { @@ -63,7 +63,28 @@ export class UIServerFactory { return new UIHttpServer(uiServerConfiguration) case ApplicationProtocol.WS: default: + if ( + !Object.values(ApplicationProtocol).includes( + uiServerConfiguration.type as ApplicationProtocol + ) + ) { + // eslint-disable-next-line @typescript-eslint/no-base-to-string + const logMsg = `Unknown application protocol type '${uiServerConfiguration.type}' in '${ConfigurationSection.uiServer}' configuration section from values '${ApplicationProtocol.toString()}', defaulting to ${ + ApplicationProtocol.WS + }` + logger.warn(`${UIServerFactory.logPrefix()} ${logMsg}`) + console.warn(logMsg) + } return new UIWebSocketServer(uiServerConfiguration) } } + + private static readonly logPrefix = (modName?: string, methodName?: string): string => { + const logMsgPrefix = 'UI Server' + const logMsg = + modName != null && methodName != null + ? ` ${logMsgPrefix} | ${modName}.${methodName}:` + : ` ${logMsgPrefix} |` + return logPrefix(logMsg) + } }