From: Jérôme Benoit Date: Mon, 23 Jan 2023 13:49:47 +0000 (+0100) Subject: Fix file error handling propagation X-Git-Tag: v1.1.92~26 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=6d8b0b0e25a198e2328d165b9413f6f4d13db2e7;p=e-mobility-charging-stations-simulator.git Fix file error handling propagation Signed-off-by: Jérôme Benoit --- diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index f1f43bcf..9586a868 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -422,27 +422,21 @@ export default class Configuration { params: HandleErrorParams = { throwError: true } ): void { const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : ''; - if (error.code === 'ENOENT') { - console.error( - chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' not found: '), - error - ); - } else if (error.code === 'EEXIST') { - console.error( - chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' already exists: '), - error - ); - } else if (error.code === 'EACCES') { - console.error( - chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' access denied: '), - error - ); - } else { - console.error( - chalk.green(prefix) + chalk.red(fileType + ' file ' + filePath + ' error: '), - error - ); + let logMsg: string; + switch (error.code) { + case 'ENOENT': + logMsg = `${fileType} file ${filePath} not found: `; + break; + case 'EEXIST': + logMsg = `${fileType} file ${filePath} already exists: `; + break; + case 'EACCES': + logMsg = `${fileType} file ${filePath} access denied: `; + break; + default: + logMsg = `${fileType} file ${filePath} error: `; } + console.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error); if (params?.throwError) { throw error; } diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index 0efccbc3..87a8733b 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -22,7 +22,7 @@ export default class FileUtils { listener: fs.WatchListener = (event, filename) => { if (filename && event === 'change') { try { - logger.debug(logPrefix + ' ' + fileType + ' file ' + file + ' have changed, reload'); + logger.debug(`${logPrefix} ${fileType} file ${file} + ' have changed, reload'`); refreshedVariable && (refreshedVariable = JSON.parse(fs.readFileSync(file, 'utf8')) as T); } catch (error) { FileUtils.handleFileException(logPrefix, fileType, file, error as NodeJS.ErrnoException, { @@ -53,45 +53,28 @@ export default class FileUtils { params: HandleErrorParams = { throwError: true, consoleOut: false } ): void { const prefix = !Utils.isEmptyString(logPrefix) ? logPrefix + ' ' : ''; - if (error.code === 'ENOENT') { - if (params?.consoleOut) { - console.warn( - chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' not found: '), - error - ); - } else { - logger.warn(prefix + fileType + ' file ' + file + ' not found:', error); - } - } else if (error.code === 'EEXIST') { - if (params?.consoleOut) { - console.warn( - chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' already exists: '), - error - ); - } else { - logger.warn(prefix + fileType + ' file ' + file + ' already exists:', error); - } - } else if (error.code === 'EACCES') { - if (params?.consoleOut) { - console.warn( - chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' access denied: '), - error - ); - } else { - logger.warn(prefix + fileType + ' file ' + file + ' access denied:', error); - } + let logMsg: string; + switch (error.code) { + case 'ENOENT': + logMsg = `${fileType} file ${file} not found:`; + break; + case 'EEXIST': + logMsg = `${fileType} file ${file} already exists:`; + break; + case 'EACCES': + logMsg = `${fileType} file ${file} access denied:`; + break; + default: + logMsg = `${fileType} file ${file} error:`; + } + if (params?.consoleOut) { + logMsg = `${logMsg} `; + console.warn(`${chalk.green(prefix)}${chalk.yellow(logMsg)}`, error); } else { - if (params?.consoleOut) { - console.warn( - chalk.green(prefix) + chalk.yellow(fileType + ' file ' + file + ' error: '), - error - ); - } else { - logger.warn(prefix + fileType + ' file ' + file + ' error:', error); - } - if (params?.throwError) { - throw error; - } + logger.warn(`${prefix}${logMsg}`, error); + } + if (params?.throwError) { + throw error; } } }