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