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