Merge pull request #836 from SAP/dependabot/npm_and_yarn/tsx-4.0.0
[e-mobility-charging-stations-simulator.git] / src / utils / ErrorUtils.ts
CommitLineData
51022aa0
JB
1import chalk from 'chalk';
2
3import { logger } from './Logger';
9bf0ef23 4import { isNotEmptyString } from './Utils';
7671fa0b
JB
5import type { ChargingStation } from '../charging-station';
6import type {
7 EmptyObject,
8 FileType,
9 HandleErrorParams,
10 IncomingRequestCommand,
7b5dbe91 11 JsonType,
7671fa0b
JB
12 RequestCommand,
13} from '../types';
51022aa0 14
1c34e5f9
JB
15const defaultErrorParams = {
16 throwError: true,
17 consoleOut: false,
18};
19
fa5995d6
JB
20export const handleUncaughtException = (): void => {
21 process.on('uncaughtException', (error: Error) => {
22 console.error(chalk.red('Uncaught exception: '), error);
23 });
24};
51022aa0 25
fa5995d6
JB
26export const handleUnhandledRejection = (): void => {
27 process.on('unhandledRejection', (reason: unknown) => {
28 console.error(chalk.red('Unhandled rejection: '), reason);
29 });
30};
7671fa0b 31
fa5995d6
JB
32export const handleFileException = (
33 file: string,
34 fileType: FileType,
35 error: NodeJS.ErrnoException,
36 logPrefix: string,
5edd8ba0 37 params: HandleErrorParams<EmptyObject> = defaultErrorParams,
fa5995d6
JB
38): void => {
39 setDefaultErrorParams(params);
9bf0ef23 40 const prefix = isNotEmptyString(logPrefix) ? `${logPrefix} ` : '';
fa5995d6
JB
41 let logMsg: string;
42 switch (error.code) {
43 case 'ENOENT':
44 logMsg = `${fileType} file ${file} not found:`;
45 break;
46 case 'EEXIST':
47 logMsg = `${fileType} file ${file} already exists:`;
48 break;
49 case 'EACCES':
50 logMsg = `${fileType} file ${file} access denied:`;
51 break;
52 case 'EPERM':
53 logMsg = `${fileType} file ${file} permission denied:`;
54 break;
55 default:
56 logMsg = `${fileType} file ${file} error:`;
7671fa0b 57 }
fa5995d6 58 if (params?.consoleOut === true) {
5587f482 59 logMsg = `${logMsg} `;
fa5995d6 60 if (params?.throwError) {
5587f482 61 console.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error);
fa5995d6 62 } else {
5587f482 63 console.warn(`${chalk.green(prefix)}${chalk.yellow(logMsg)}`, error);
51022aa0 64 }
fa5995d6 65 } else if (params?.consoleOut === false) {
51022aa0 66 if (params?.throwError) {
fa5995d6
JB
67 logger.error(`${prefix}${logMsg}`, error);
68 } else {
69 logger.warn(`${prefix}${logMsg}`, error);
51022aa0
JB
70 }
71 }
fa5995d6
JB
72 if (params?.throwError) {
73 throw error;
7671fa0b 74 }
fa5995d6 75};
7b5dbe91 76
fa5995d6
JB
77export const handleSendMessageError = (
78 chargingStation: ChargingStation,
79 commandName: RequestCommand | IncomingRequestCommand,
80 error: Error,
5edd8ba0 81 params: HandleErrorParams<EmptyObject> = { throwError: false, consoleOut: false },
fa5995d6
JB
82): void => {
83 setDefaultErrorParams(params, { throwError: false, consoleOut: false });
84 logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error);
85 if (params?.throwError === true) {
86 throw error;
7b5dbe91 87 }
fa5995d6
JB
88};
89
90export const setDefaultErrorParams = <T extends JsonType>(
91 params: HandleErrorParams<T>,
5edd8ba0 92 defaultParams: HandleErrorParams<T> = defaultErrorParams,
fa5995d6
JB
93): HandleErrorParams<T> => {
94 params = { ...defaultParams, ...params };
95 return params;
96};