Enforce singleton design pattern for the logger
[e-mobility-charging-stations-simulator.git] / src / utils / FileUtils.ts
CommitLineData
8eac9a09 1import chalk from 'chalk';
bc464bb1 2import getLogger from './Logger';
23132a44
JB
3
4export default class FileUtils {
5 static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException, consoleOut = false): void {
6 const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : '';
7 if (error.code === 'ENOENT') {
8 if (consoleOut) {
e4362ed7 9 console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' not found: '), error);
23132a44 10 } else {
bc464bb1 11 getLogger().warn(prefix + fileType + ' file ' + filePath + ' not found: %j', error);
23132a44 12 }
72f041bd
JB
13 } else if (error.code === 'EEXIST') {
14 if (consoleOut) {
e4362ed7 15 console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' already exists: '), error);
72f041bd 16 } else {
bc464bb1 17 getLogger().warn(prefix + fileType + ' file ' + filePath + ' already exists: %j', error);
72f041bd
JB
18 }
19 } else if (error.code === 'EACCES') {
20 if (consoleOut) {
e4362ed7 21 console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' access denied: '), error);
72f041bd 22 } else {
bc464bb1 23 getLogger().warn(prefix + fileType + ' file ' + filePath + ' access denied: %j', error);
72f041bd 24 }
23132a44
JB
25 } else {
26 if (consoleOut) {
e4362ed7 27 console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' error: '), error);
23132a44 28 } else {
bc464bb1 29 getLogger().warn(prefix + fileType + ' file ' + filePath + ' error: %j', error);
23132a44
JB
30 }
31 throw error;
32 }
33 }
34}