import { logger } from './Logger';
import { Utils } from './Utils';
-import type { EmptyObject, FileType, HandleErrorParams } from '../types';
+import type { ChargingStation } from '../charging-station';
+import type {
+ EmptyObject,
+ FileType,
+ HandleErrorParams,
+ IncomingRequestCommand,
+ RequestCommand,
+} from '../types';
export class ErrorUtils {
private constructor() {
// This is intentional
}
+ public static handleUncaughtException(): void {
+ process.on('uncaughtException', (error: Error) => {
+ console.error(chalk.red('Uncaught exception: '), error);
+ });
+ }
+
+ public static handleUnhandledRejection(): void {
+ process.on('unhandledRejection', (reason: unknown) => {
+ console.error(chalk.red('Unhandled rejection: '), reason);
+ });
+ }
+
public static handleFileException(
file: string,
fileType: FileType,
throw error;
}
}
+
+ public static 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;
+ }
+ }
}