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