1 import chalk from
'chalk';
3 import { logger
} from
'./Logger';
4 import { Utils
} from
'./Utils';
5 import type { ChargingStation
} from
'../charging-station';
10 IncomingRequestCommand
,
15 export class ErrorUtils
{
16 private constructor() {
17 // This is intentional
20 public static handleUncaughtException(): void {
21 process
.on('uncaughtException', (error
: Error) => {
22 console
.error(chalk
.red('Uncaught exception: '), error
);
26 public static handleUnhandledRejection(): void {
27 process
.on('unhandledRejection', (reason
: unknown
) => {
28 console
.error(chalk
.red('Unhandled rejection: '), reason
);
32 public static handleFileException(
35 error
: NodeJS
.ErrnoException
,
37 params
: HandleErrorParams
<EmptyObject
> = { throwError
: true, consoleOut
: false }
39 ErrorUtils
.handleErrorParams(params
);
40 const prefix
= Utils
.isNotEmptyString(logPrefix
) ? `${logPrefix} ` : '';
44 logMsg
= `${fileType} file ${file} not found:`;
47 logMsg
= `${fileType} file ${file} already exists:`;
50 logMsg
= `${fileType} file ${file} access denied:`;
53 logMsg
= `${fileType} file ${file} permission denied:`;
56 logMsg
= `${fileType} file ${file} error:`;
58 if (params
?.consoleOut
=== true) {
59 if (params
?.throwError
) {
60 console
.error(`${chalk.green(prefix)}${chalk.red(`${logMsg} `)}`, error);
62 console.warn(`${chalk.green(prefix)}${chalk.yellow(`${logMsg}
`)}`, error
);
64 } else if (params
?.consoleOut
=== false) {
65 if (params
?.throwError
) {
66 logger
.error(`${prefix}${logMsg}`, error
);
68 logger
.warn(`${prefix}${logMsg}`, error
);
71 if (params
?.throwError
) {
76 public static handleSendMessageError(
77 chargingStation
: ChargingStation
,
78 commandName
: RequestCommand
| IncomingRequestCommand
,
80 params
: HandleErrorParams
<EmptyObject
> = { throwError
: false, consoleOut
: false }
82 ErrorUtils
.handleErrorParams(params
, { throwError
: false, consoleOut
: false });
83 logger
.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error
);
84 if (params
?.throwError
=== true) {
89 public static handleErrorParams
<T
extends JsonType
>(
90 params
: HandleErrorParams
<T
>,
91 defaultParams
: HandleErrorParams
<T
> = { throwError
: true, consoleOut
: false }
92 ): HandleErrorParams
<T
> {
93 return { ...defaultParams
, ...params
};