type StationTemplateUrl,
type Statistics,
} from '../types';
-import { Configuration, Constants, ErrorUtils, Utils, logger } from '../utils';
+import {
+ Configuration,
+ Constants,
+ Utils,
+ handleUncaughtException,
+ handleUnhandledRejection,
+ logger,
+} from '../utils';
import { type MessageHandler, type WorkerAbstract, WorkerFactory } from '../worker';
const moduleName = 'Bootstrap';
process.on(signal, this.gracefulShutdown);
}
// Enable unconditionally for now
- ErrorUtils.handleUnhandledRejection();
- ErrorUtils.handleUncaughtException();
+ handleUnhandledRejection();
+ handleUncaughtException();
this.initializedCounters = false;
this.started = false;
this.initializeCounters();
Configuration,
Constants,
DCElectricUtils,
- ErrorUtils,
- FileUtils,
MessageChannelUtils,
Utils,
buildChargingStationAutomaticTransactionGeneratorConfiguration,
buildConnectorsStatus,
buildEvsesStatus,
+ handleFileException,
logger,
+ watchJsonFile,
} from '../utils';
export class ChargingStation {
}
this.openWSConnection();
// Monitor charging station template file
- this.templateFileWatcher = FileUtils.watchJsonFile(
+ this.templateFileWatcher = watchJsonFile(
this.templateFile,
FileType.ChargingStationTemplate,
this.logPrefix(),
this.templateFileHash = template.templateHash;
}
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
this.templateFile,
FileType.ChargingStationTemplate,
error as NodeJS.ErrnoException,
this.configurationFileHash = configuration.configurationHash;
}
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
this.configurationFile,
FileType.ChargingStationConfiguration,
error as NodeJS.ErrnoException,
this.configurationFileHash = configurationHash;
})
.catch((error) => {
- ErrorUtils.handleFileException(
+ handleFileException(
this.configurationFile,
FileType.ChargingStationConfiguration,
error as NodeJS.ErrnoException,
);
}
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
this.configurationFile,
FileType.ChargingStationConfiguration,
error as NodeJS.ErrnoException,
import type { ChargingStation } from './ChargingStation';
import { ChargingStationUtils } from './ChargingStationUtils';
import { FileType, IdTagDistribution } from '../types';
-import { ErrorUtils, FileUtils, Utils, logger } from '../utils';
+import { Utils, handleFileException, logger, watchJsonFile } from '../utils';
type IdTagsCacheValueType = {
idTags: string[];
private setIdTagsCache(file: string, idTags: string[]) {
return this.idTagsCaches.set(file, {
idTags,
- idTagsFileWatcher: FileUtils.watchJsonFile(
+ idTagsFileWatcher: watchJsonFile(
file,
FileType.Authorization,
this.logPrefix(file),
this.deleteIdTagsCache(file);
this.deleteIdTagsCacheIndexes(file);
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
file,
FileType.Authorization,
error as NodeJS.ErrnoException,
try {
return JSON.parse(fs.readFileSync(file, 'utf8')) as string[];
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
file,
FileType.Authorization,
error as NodeJS.ErrnoException,
JsonType,
OCPPVersion,
} from '../../types';
-import { ErrorUtils, logger } from '../../utils';
+import { logger, setDefaultErrorParams } from '../../utils';
const moduleName = 'OCPPIncomingRequestService';
error: Error,
params: HandleErrorParams<T> = { throwError: true, consoleOut: false }
): T | undefined {
- ErrorUtils.setDefaultErrorParams(params);
+ setDefaultErrorParams(params);
logger.error(
`${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
error
type ResponseCallback,
type ResponseType,
} from '../../types';
-import { Constants, ErrorUtils, Utils, logger } from '../../utils';
+import { Constants, Utils, handleSendMessageError, logger } from '../../utils';
const moduleName = 'OCPPRequestService';
commandName
);
} catch (error) {
- ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error, {
+ handleSendMessageError(chargingStation, commandName, error as Error, {
throwError: true,
});
}
commandName
);
} catch (error) {
- ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error);
+ handleSendMessageError(chargingStation, commandName, error as Error);
}
}
params
);
} catch (error) {
- ErrorUtils.handleSendMessageError(chargingStation, commandName, error as Error, {
+ handleSendMessageError(chargingStation, commandName, error as Error, {
throwError: params.throwError,
});
}
type StatusNotificationRequest,
type StatusNotificationResponse,
} from '../../types';
-import { ErrorUtils, Utils, logger } from '../../utils';
+import { Utils, handleFileException, logger } from '../../utils';
export class OCPPServiceUtils {
protected constructor() {
try {
return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType<T>;
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
filePath,
FileType.JsonSchema,
error as NodeJS.ErrnoException,
import { Storage } from './Storage';
import { BaseError } from '../../exception';
import { FileType, type Statistics } from '../../types';
-import { AsyncLock, AsyncLockType, Constants, ErrorUtils, Utils } from '../../utils';
+import { AsyncLock, AsyncLockType, Constants, Utils, handleFileException } from '../../utils';
export class JsonFileStorage extends Storage {
private fd: number | null = null;
);
})
.catch((error) => {
- ErrorUtils.handleFileException(
+ handleFileException(
this.dbName,
FileType.PerformanceRecords,
error as NodeJS.ErrnoException,
this.fd = fs.openSync(this.dbName, 'a+');
}
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
this.dbName,
FileType.PerformanceRecords,
error as NodeJS.ErrnoException,
this.fd = null;
}
} catch (error) {
- ErrorUtils.handleFileException(
+ handleFileException(
this.dbName,
FileType.PerformanceRecords,
error as NodeJS.ErrnoException,
type Statistics,
StorageType,
} from '../../types';
-import { ErrorUtils, Utils, logger } from '../../utils';
+import { Utils, logger, setDefaultErrorParams } from '../../utils';
export abstract class Storage {
protected readonly storageUri: URL;
table?: string,
params: HandleErrorParams<EmptyObject> = { throwError: false, consoleOut: false }
): void {
- ErrorUtils.setDefaultErrorParams(params, { throwError: false, consoleOut: false });
+ setDefaultErrorParams(params, { throwError: false, consoleOut: false });
const inTableOrCollectionStr =
(!Utils.isNullOrUndefined(table) || !table) && ` in table or collection '${table}'`;
logger.error(
consoleOut: false,
};
-export class ErrorUtils {
- private constructor() {
- // This is intentional
- }
+export const handleUncaughtException = (): void => {
+ process.on('uncaughtException', (error: Error) => {
+ console.error(chalk.red('Uncaught exception: '), error);
+ });
+};
- public static handleUncaughtException(): void {
- process.on('uncaughtException', (error: Error) => {
- console.error(chalk.red('Uncaught exception: '), error);
- });
- }
+export const handleUnhandledRejection = (): void => {
+ process.on('unhandledRejection', (reason: unknown) => {
+ console.error(chalk.red('Unhandled rejection: '), reason);
+ });
+};
- public static handleUnhandledRejection(): void {
- process.on('unhandledRejection', (reason: unknown) => {
- console.error(chalk.red('Unhandled rejection: '), reason);
- });
+export const handleFileException = (
+ file: string,
+ fileType: FileType,
+ error: NodeJS.ErrnoException,
+ logPrefix: string,
+ params: HandleErrorParams<EmptyObject> = defaultErrorParams
+): void => {
+ setDefaultErrorParams(params);
+ const prefix = Utils.isNotEmptyString(logPrefix) ? `${logPrefix} ` : '';
+ let logMsg: string;
+ switch (error.code) {
+ case 'ENOENT':
+ logMsg = `${fileType} file ${file} not found:`;
+ break;
+ case 'EEXIST':
+ logMsg = `${fileType} file ${file} already exists:`;
+ break;
+ case 'EACCES':
+ logMsg = `${fileType} file ${file} access denied:`;
+ break;
+ case 'EPERM':
+ logMsg = `${fileType} file ${file} permission denied:`;
+ break;
+ default:
+ logMsg = `${fileType} file ${file} error:`;
}
-
- public static handleFileException(
- file: string,
- fileType: FileType,
- error: NodeJS.ErrnoException,
- logPrefix: string,
- params: HandleErrorParams<EmptyObject> = defaultErrorParams
- ): void {
- ErrorUtils.setDefaultErrorParams(params);
- const prefix = Utils.isNotEmptyString(logPrefix) ? `${logPrefix} ` : '';
- let logMsg: string;
- switch (error.code) {
- case 'ENOENT':
- logMsg = `${fileType} file ${file} not found:`;
- break;
- case 'EEXIST':
- logMsg = `${fileType} file ${file} already exists:`;
- break;
- case 'EACCES':
- logMsg = `${fileType} file ${file} access denied:`;
- break;
- case 'EPERM':
- logMsg = `${fileType} file ${file} permission denied:`;
- break;
- default:
- logMsg = `${fileType} file ${file} error:`;
- }
- if (params?.consoleOut === true) {
- if (params?.throwError) {
- console.error(`${chalk.green(prefix)}${chalk.red(`${logMsg} `)}`, error);
- } else {
- console.warn(`${chalk.green(prefix)}${chalk.yellow(`${logMsg} `)}`, error);
- }
- } else if (params?.consoleOut === false) {
- if (params?.throwError) {
- logger.error(`${prefix}${logMsg}`, error);
- } else {
- logger.warn(`${prefix}${logMsg}`, error);
- }
+ if (params?.consoleOut === true) {
+ if (params?.throwError) {
+ console.error(`${chalk.green(prefix)}${chalk.red(`${logMsg} `)}`, error);
+ } else {
+ console.warn(`${chalk.green(prefix)}${chalk.yellow(`${logMsg} `)}`, error);
}
+ } else if (params?.consoleOut === false) {
if (params?.throwError) {
- throw error;
+ logger.error(`${prefix}${logMsg}`, error);
+ } else {
+ logger.warn(`${prefix}${logMsg}`, error);
}
}
-
- public static handleSendMessageError(
- chargingStation: ChargingStation,
- commandName: RequestCommand | IncomingRequestCommand,
- error: Error,
- params: HandleErrorParams<EmptyObject> = { throwError: false, consoleOut: false }
- ): void {
- ErrorUtils.setDefaultErrorParams(params, { throwError: false, consoleOut: false });
- logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error);
- if (params?.throwError === true) {
- throw error;
- }
+ if (params?.throwError) {
+ throw error;
}
+};
- public static setDefaultErrorParams<T extends JsonType>(
- params: HandleErrorParams<T>,
- defaultParams: HandleErrorParams<T> = defaultErrorParams
- ): HandleErrorParams<T> {
- params = { ...defaultParams, ...params };
- return params;
+export const handleSendMessageError = (
+ chargingStation: ChargingStation,
+ commandName: RequestCommand | IncomingRequestCommand,
+ error: Error,
+ params: HandleErrorParams<EmptyObject> = { throwError: false, consoleOut: false }
+): void => {
+ setDefaultErrorParams(params, { throwError: false, consoleOut: false });
+ logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error);
+ if (params?.throwError === true) {
+ throw error;
}
-}
+};
+
+export const setDefaultErrorParams = <T extends JsonType>(
+ params: HandleErrorParams<T>,
+ defaultParams: HandleErrorParams<T> = defaultErrorParams
+): HandleErrorParams<T> => {
+ params = { ...defaultParams, ...params };
+ return params;
+};
import fs from 'node:fs';
-import { ErrorUtils } from './ErrorUtils';
+import { handleFileException } from './ErrorUtils';
import { logger } from './Logger';
import { Utils } from './Utils';
import type { FileType, JsonType } from '../types';
-export class FileUtils {
- private constructor() {
- // This is intentional
- }
-
- public static watchJsonFile<T extends JsonType>(
- file: string,
- fileType: FileType,
- logPrefix: string,
- refreshedVariable?: T,
- listener: fs.WatchListener<string> = (event, filename) => {
- if (Utils.isNotEmptyString(filename) && event === 'change') {
- try {
- logger.debug(`${logPrefix} ${fileType} file ${file} have changed, reload`);
- refreshedVariable && (refreshedVariable = JSON.parse(fs.readFileSync(file, 'utf8')) as T);
- } catch (error) {
- ErrorUtils.handleFileException(
- file,
- fileType,
- error as NodeJS.ErrnoException,
- logPrefix,
- {
- throwError: false,
- }
- );
- }
- }
- }
- ): fs.FSWatcher | undefined {
- if (Utils.isNotEmptyString(file)) {
+export const watchJsonFile = <T extends JsonType>(
+ file: string,
+ fileType: FileType,
+ logPrefix: string,
+ refreshedVariable?: T,
+ listener: fs.WatchListener<string> = (event, filename) => {
+ if (Utils.isNotEmptyString(filename) && event === 'change') {
try {
- return fs.watch(file, listener);
+ logger.debug(`${logPrefix} ${fileType} file ${file} have changed, reload`);
+ refreshedVariable && (refreshedVariable = JSON.parse(fs.readFileSync(file, 'utf8')) as T);
} catch (error) {
- ErrorUtils.handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, {
+ handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, {
throwError: false,
});
}
- } else {
- logger.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`);
}
}
-}
+): fs.FSWatcher | undefined => {
+ if (Utils.isNotEmptyString(file)) {
+ try {
+ return fs.watch(file, listener);
+ } catch (error) {
+ handleFileException(file, fileType, error as NodeJS.ErrnoException, logPrefix, {
+ throwError: false,
+ });
+ }
+ } else {
+ logger.info(`${logPrefix} No ${fileType} file to watch given. Not monitoring its changes`);
+ }
+};
export { CircularArray } from './CircularArray';
export { Configuration } from './Configuration';
export { Constants } from './Constants';
-export { ErrorUtils } from './ErrorUtils';
-export { FileUtils } from './FileUtils';
+export {
+ handleFileException,
+ handleUncaughtException,
+ handleUnhandledRejection,
+ handleSendMessageError,
+ setDefaultErrorParams,
+} from './ErrorUtils';
+export { watchJsonFile } from './FileUtils';
export { MessageChannelUtils } from './MessageChannelUtils';
export { Utils } from './Utils';
export { logger } from './Logger';