1 import chalk from
'chalk';
3 import { logger
} from
'./Logger';
4 import { Utils
} from
'./Utils';
5 import type { ChargingStation
} from
'../charging-station';
10 IncomingRequestCommand
,
14 export class ErrorUtils
{
15 private constructor() {
16 // This is intentional
19 public static handleUncaughtException(): void {
20 process
.on('uncaughtException', (error
: Error) => {
21 console
.error(chalk
.red('Uncaught exception: '), error
);
25 public static handleUnhandledRejection(): void {
26 process
.on('unhandledRejection', (reason
: unknown
) => {
27 console
.error(chalk
.red('Unhandled rejection: '), reason
);
31 public static handleFileException(
34 error
: NodeJS
.ErrnoException
,
36 params
: HandleErrorParams
<EmptyObject
> = { throwError
: true, consoleOut
: false }
38 const prefix
= Utils
.isNotEmptyString(logPrefix
) ? `${logPrefix} ` : '';
42 logMsg
= `${fileType} file ${file} not found:`;
45 logMsg
= `${fileType} file ${file} already exists:`;
48 logMsg
= `${fileType} file ${file} access denied:`;
51 logMsg
= `${fileType} file ${file} error:`;
53 if (params
?.consoleOut
) {
54 console
.warn(`${chalk.green(prefix)}${chalk.yellow(`${logMsg} `)}`, error);
56 logger.warn(`${prefix}${logMsg}
`, error);
58 if (params?.throwError) {
63 public static handleSendMessageError(
64 chargingStation: ChargingStation,
65 commandName: RequestCommand | IncomingRequestCommand,
67 params: HandleErrorParams<EmptyObject> = { throwError: false }
69 logger.error(`${chargingStation.logPrefix()} Request command
'${commandName}' error
:`, error);
70 if (params?.throwError === true) {