build(simulator): don't preserve modules
[e-mobility-charging-stations-simulator.git] / src / utils / ErrorUtils.ts
1 import chalk from 'chalk';
2
3 import { logger } from './Logger';
4 import { Utils } from './Utils';
5 import type { EmptyObject, FileType, HandleErrorParams } from '../types';
6
7 export class ErrorUtils {
8 private constructor() {
9 // This is intentional
10 }
11
12 public static handleFileException(
13 file: string,
14 fileType: FileType,
15 error: NodeJS.ErrnoException,
16 logPrefix: string,
17 params: HandleErrorParams<EmptyObject> = { throwError: true, consoleOut: false }
18 ): void {
19 const prefix = Utils.isNotEmptyString(logPrefix) ? `${logPrefix} ` : '';
20 let logMsg: string;
21 switch (error.code) {
22 case 'ENOENT':
23 logMsg = `${fileType} file ${file} not found:`;
24 break;
25 case 'EEXIST':
26 logMsg = `${fileType} file ${file} already exists:`;
27 break;
28 case 'EACCES':
29 logMsg = `${fileType} file ${file} access denied:`;
30 break;
31 default:
32 logMsg = `${fileType} file ${file} error:`;
33 }
34 if (params?.consoleOut) {
35 console.warn(`${chalk.green(prefix)}${chalk.yellow(`${logMsg} `)}`, error);
36 } else {
37 logger.warn(`${prefix}${logMsg}`, error);
38 }
39 if (params?.throwError) {
40 throw error;
41 }
42 }
43 }