1 import chalk from
'chalk';
3 import { logger
} from
'./Logger';
4 import { isNotEmptyString
} from
'./Utils';
5 import type { ChargingStation
} from
'../charging-station';
10 IncomingRequestCommand
,
15 const defaultErrorParams
= {
20 export const handleUncaughtException
= (): void => {
21 process
.on('uncaughtException', (error
: Error) => {
22 console
.error(chalk
.red('Uncaught exception: '), error
);
26 export const handleUnhandledRejection
= (): void => {
27 process
.on('unhandledRejection', (reason
: unknown
) => {
28 console
.error(chalk
.red('Unhandled rejection: '), reason
);
32 export const handleFileException
= (
35 error
: NodeJS
.ErrnoException
,
37 params
: HandleErrorParams
<EmptyObject
> = defaultErrorParams
,
39 setDefaultErrorParams(params
);
40 const prefix
= 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 logMsg
= `${logMsg} `;
60 if (params
?.throwError
) {
61 console
.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error
);
63 console
.warn(`${chalk.green(prefix)}${chalk.yellow(logMsg)}`, error
);
65 } else if (params
?.consoleOut
=== false) {
66 if (params
?.throwError
) {
67 logger
.error(`${prefix}${logMsg}`, error
);
69 logger
.warn(`${prefix}${logMsg}`, error
);
72 if (params
?.throwError
) {
77 export const handleSendMessageError
= (
78 chargingStation
: ChargingStation
,
79 commandName
: RequestCommand
| IncomingRequestCommand
,
81 params
: HandleErrorParams
<EmptyObject
> = { throwError
: false, consoleOut
: false },
83 setDefaultErrorParams(params
, { throwError
: false, consoleOut
: false });
84 logger
.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error
);
85 if (params
?.throwError
=== true) {
90 export const setDefaultErrorParams
= <T
extends JsonType
>(
91 params
: HandleErrorParams
<T
>,
92 defaultParams
: HandleErrorParams
<T
> = defaultErrorParams
,
93 ): HandleErrorParams
<T
> => {
94 params
= { ...defaultParams
, ...params
};