fix: various fixes to files handling and their content caching
[e-mobility-charging-stations-simulator.git] / src / utils / ErrorUtils.ts
CommitLineData
51022aa0
JB
1import chalk from 'chalk';
2
3import { logger } from './Logger';
4import { Utils } 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
JB
14
15export class ErrorUtils {
16 private constructor() {
17 // This is intentional
18 }
19
7671fa0b
JB
20 public static handleUncaughtException(): void {
21 process.on('uncaughtException', (error: Error) => {
22 console.error(chalk.red('Uncaught exception: '), error);
23 });
24 }
25
26 public static handleUnhandledRejection(): void {
27 process.on('unhandledRejection', (reason: unknown) => {
28 console.error(chalk.red('Unhandled rejection: '), reason);
29 });
30 }
31
51022aa0
JB
32 public static handleFileException(
33 file: string,
34 fileType: FileType,
35 error: NodeJS.ErrnoException,
36 logPrefix: string,
37 params: HandleErrorParams<EmptyObject> = { throwError: true, consoleOut: false }
38 ): void {
7b5dbe91 39 ErrorUtils.handleErrorParams(params);
51022aa0
JB
40 const prefix = Utils.isNotEmptyString(logPrefix) ? `${logPrefix} ` : '';
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;
7b5dbe91
JB
52 case 'EPERM':
53 logMsg = `${fileType} file ${file} permission denied:`;
54 break;
51022aa0
JB
55 default:
56 logMsg = `${fileType} file ${file} error:`;
57 }
7b5dbe91
JB
58 if (params?.consoleOut === true) {
59 if (params?.throwError) {
60 console.error(`${chalk.green(prefix)}${chalk.red(`${logMsg} `)}`, error);
61 } else {
62 console.warn(`${chalk.green(prefix)}${chalk.yellow(`${logMsg} `)}`, error);
63 }
64 } else if (params?.consoleOut === false) {
65 if (params?.throwError) {
66 logger.error(`${prefix}${logMsg}`, error);
67 } else {
68 logger.warn(`${prefix}${logMsg}`, error);
69 }
51022aa0
JB
70 }
71 if (params?.throwError) {
72 throw error;
73 }
74 }
7671fa0b
JB
75
76 public static handleSendMessageError(
77 chargingStation: ChargingStation,
78 commandName: RequestCommand | IncomingRequestCommand,
79 error: Error,
7b5dbe91 80 params: HandleErrorParams<EmptyObject> = { throwError: false, consoleOut: false }
7671fa0b 81 ): void {
7b5dbe91 82 ErrorUtils.handleErrorParams(params, { throwError: false, consoleOut: false });
7671fa0b
JB
83 logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error);
84 if (params?.throwError === true) {
85 throw error;
86 }
87 }
7b5dbe91
JB
88
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 };
94 }
51022aa0 95}