Performance statistics: add JSON file storage support.
[e-mobility-charging-stations-simulator.git] / src / utils / FileUtils.ts
1 import logger from './Logger';
2
3 export 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) {
8 console.warn(prefix + fileType + ' file ' + filePath + ' not found: ', error);
9 } else {
10 logger.warn(prefix + fileType + ' file ' + filePath + ' not found: %j', error);
11 }
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 }
24 } else {
25 if (consoleOut) {
26 console.error(prefix + fileType + ' file ' + filePath + ' error: ', error);
27 } else {
28 logger.error(prefix + fileType + ' file ' + filePath + ' error: %j', error);
29 }
30 throw error;
31 }
32 }
33 }