build(deps-dev): apply updates
[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 14
1c34e5f9
JB
15const defaultErrorParams = {
16 throwError: true,
17 consoleOut: false,
18};
19
51022aa0
JB
20export class ErrorUtils {
21 private constructor() {
22 // This is intentional
23 }
24
7671fa0b
JB
25 public static handleUncaughtException(): void {
26 process.on('uncaughtException', (error: Error) => {
27 console.error(chalk.red('Uncaught exception: '), error);
28 });
29 }
30
31 public static handleUnhandledRejection(): void {
32 process.on('unhandledRejection', (reason: unknown) => {
33 console.error(chalk.red('Unhandled rejection: '), reason);
34 });
35 }
36
51022aa0
JB
37 public static handleFileException(
38 file: string,
39 fileType: FileType,
40 error: NodeJS.ErrnoException,
41 logPrefix: string,
1c34e5f9 42 params: HandleErrorParams<EmptyObject> = defaultErrorParams
51022aa0 43 ): void {
1c34e5f9 44 ErrorUtils.setDefaultErrorParams(params);
51022aa0
JB
45 const prefix = Utils.isNotEmptyString(logPrefix) ? `${logPrefix} ` : '';
46 let logMsg: string;
47 switch (error.code) {
48 case 'ENOENT':
49 logMsg = `${fileType} file ${file} not found:`;
50 break;
51 case 'EEXIST':
52 logMsg = `${fileType} file ${file} already exists:`;
53 break;
54 case 'EACCES':
55 logMsg = `${fileType} file ${file} access denied:`;
56 break;
7b5dbe91
JB
57 case 'EPERM':
58 logMsg = `${fileType} file ${file} permission denied:`;
59 break;
51022aa0
JB
60 default:
61 logMsg = `${fileType} file ${file} error:`;
62 }
7b5dbe91
JB
63 if (params?.consoleOut === true) {
64 if (params?.throwError) {
65 console.error(`${chalk.green(prefix)}${chalk.red(`${logMsg} `)}`, error);
66 } else {
67 console.warn(`${chalk.green(prefix)}${chalk.yellow(`${logMsg} `)}`, error);
68 }
69 } else if (params?.consoleOut === false) {
70 if (params?.throwError) {
71 logger.error(`${prefix}${logMsg}`, error);
72 } else {
73 logger.warn(`${prefix}${logMsg}`, error);
74 }
51022aa0
JB
75 }
76 if (params?.throwError) {
77 throw error;
78 }
79 }
7671fa0b
JB
80
81 public static handleSendMessageError(
82 chargingStation: ChargingStation,
83 commandName: RequestCommand | IncomingRequestCommand,
84 error: Error,
7b5dbe91 85 params: HandleErrorParams<EmptyObject> = { throwError: false, consoleOut: false }
7671fa0b 86 ): void {
1c34e5f9 87 ErrorUtils.setDefaultErrorParams(params, { throwError: false, consoleOut: false });
7671fa0b
JB
88 logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error);
89 if (params?.throwError === true) {
90 throw error;
91 }
92 }
7b5dbe91 93
1c34e5f9 94 public static setDefaultErrorParams<T extends JsonType>(
7b5dbe91 95 params: HandleErrorParams<T>,
1c34e5f9 96 defaultParams: HandleErrorParams<T> = defaultErrorParams
7b5dbe91 97 ): HandleErrorParams<T> {
a9c8f6cb
JB
98 params = { ...defaultParams, ...params };
99 return params;
7b5dbe91 100 }
51022aa0 101}