Apply prettier formating
[e-mobility-charging-stations-simulator.git] / src / utils / FileUtils.ts
1 import { EmptyObject } from '../types/EmptyObject';
2 import { HandleErrorParams } from '../types/Error';
3 import chalk from 'chalk';
4 import logger from './Logger';
5
6 export default class FileUtils {
7 static handleFileException(
8 logPrefix: string,
9 fileType: string,
10 filePath: string,
11 error: NodeJS.ErrnoException,
12 params: HandleErrorParams<EmptyObject> = { throwError: true, consoleOut: false }
13 ): void {
14 const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : '';
15 if (error.code === 'ENOENT') {
16 if (params?.consoleOut) {
17 console.warn(
18 chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' not found: '),
19 error
20 );
21 } else {
22 logger.warn(prefix + fileType + ' file ' + filePath + ' not found: %j', error);
23 }
24 } else if (error.code === 'EEXIST') {
25 if (params?.consoleOut) {
26 console.warn(
27 chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' already exists: '),
28 error
29 );
30 } else {
31 logger.warn(prefix + fileType + ' file ' + filePath + ' already exists: %j', error);
32 }
33 } else if (error.code === 'EACCES') {
34 if (params?.consoleOut) {
35 console.warn(
36 chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' access denied: '),
37 error
38 );
39 } else {
40 logger.warn(prefix + fileType + ' file ' + filePath + ' access denied: %j', error);
41 }
42 } else {
43 if (params?.consoleOut) {
44 console.warn(
45 chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' error: '),
46 error
47 );
48 } else {
49 logger.warn(prefix + fileType + ' file ' + filePath + ' error: %j', error);
50 }
51 if (params?.throwError) {
52 throw error;
53 }
54 }
55 }
56 }