537aa308b41a0516e4b688c7f9b38124952d455f
1 import process from
'node:process';
3 import chalk from
'chalk';
5 import { logger
} from
'./Logger';
6 import { isNotEmptyString
} from
'./Utils';
7 import type { ChargingStation
} from
'../charging-station';
12 IncomingRequestCommand
,
17 const defaultErrorParams
= {
22 export const handleUncaughtException
= (): void => {
23 process
.on('uncaughtException', (error
: Error) => {
24 console
.error(chalk
.red('Uncaught exception: '), error
);
28 export const handleUnhandledRejection
= (): void => {
29 process
.on('unhandledRejection', (reason
: unknown
) => {
30 console
.error(chalk
.red('Unhandled rejection: '), reason
);
34 export const handleFileException
= (
37 error
: NodeJS
.ErrnoException
,
39 params
: HandleErrorParams
<EmptyObject
> = defaultErrorParams
,
41 setDefaultErrorParams(params
);
42 const prefix
= isNotEmptyString(logPrefix
) ? `${logPrefix} ` : '';
46 logMsg
= `${fileType} file ${file} not found:`;
49 logMsg
= `${fileType} file ${file} already exists:`;
52 logMsg
= `${fileType} file ${file} access denied:`;
55 logMsg
= `${fileType} file ${file} permission denied:`;
58 logMsg
= `${fileType} file ${file} error:`;
60 if (params
?.consoleOut
=== true) {
61 logMsg
= `${logMsg} `;
62 if (params
?.throwError
) {
63 console
.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error
);
65 console
.warn(`${chalk.green(prefix)}${chalk.yellow(logMsg)}`, error
);
67 } else if (params
?.consoleOut
=== false) {
68 if (params
?.throwError
) {
69 logger
.error(`${prefix}${logMsg}`, error
);
71 logger
.warn(`${prefix}${logMsg}`, error
);
74 if (params
?.throwError
) {
79 export const handleSendMessageError
= (
80 chargingStation
: ChargingStation
,
81 commandName
: RequestCommand
| IncomingRequestCommand
,
83 params
: HandleErrorParams
<EmptyObject
> = { throwError
: false, consoleOut
: false },
85 setDefaultErrorParams(params
, { throwError
: false, consoleOut
: false });
86 logger
.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error
);
87 if (params
?.throwError
=== true) {
92 export const setDefaultErrorParams
= <T
extends JsonType
>(
93 params
: HandleErrorParams
<T
>,
94 defaultParams
: HandleErrorParams
<T
> = defaultErrorParams
,
95 ): HandleErrorParams
<T
> => {
96 params
= { ...defaultParams
, ...params
};