From 84a473541f62f4c317b547760004267d59b80c97 Mon Sep 17 00:00:00 2001 From: Ryuu Mitsuki Date: Sat, 24 Aug 2024 20:53:55 +0700 Subject: [PATCH] fix(audioconv): Ensure error logging works on `quiet` enabled - Refactored `writeErrorLog` to return a promise, improving error handling when writing to the log file. - Updated the error handling within `convertAudio` to ensure that error logs are written even when the `quiet` option is enabled (set to `true`). - Refined log and console error outputs to make them conditional on the `quiet` option, improving clarity and control over error reporting. - Added better handling for the log stream, including resolving on `finish` and rejecting on `error` to ensure reliability in the logging process. --- lib/audioconv.js | 53 +++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/lib/audioconv.js b/lib/audioconv.js index 71df00c..fbd72ed 100644 --- a/lib/audioconv.js +++ b/lib/audioconv.js @@ -242,23 +242,29 @@ async function checkFfmpeg(verbose=false) { return false; } -function writeErrorLog(logFile, data, error) { - // Throw the error if the log file name is invalid - if (isNullOrUndefined(logFile) || typeof logFile !== 'string') { - throw error; - } +async function writeErrorLog(logFile, data, error) { + // Return immediately if given log file is not a string type + if (isNullOrUndefined(logFile) || typeof logFile !== 'string') return; logFile = path.join(LOGDIR, path.basename(logFile)); createDirIfNotExistSync(LOGDIR); - const logStream = fs.createWriteStream(logFile); + return new Promise((resolve, reject) => { + const logStream = fs.createWriteStream(logFile, { flags: 'a+', flush: true }); + + logStream.write(`[ERROR] ${error?.message || 'Unknown error'}${EOL}`); + logStream.write(` Input Audio: ${data?.inputAudio || 'Unknown'}${EOL}`); + logStream.write(` Output Audio: ${data?.outputAudio || 'Unknown'}${EOL}`); + logStream.write(` File Size: ${data?.inputSize / (1024 * 1024) || '0.0'} MiB${EOL}`); + logStream.write(`---------------------------------------------${EOL}`); + logStream.end(EOL); - logStream.write(`[ERROR] ${error?.message || 'Unknown error'}${EOL}`); - logStream.write(` Input Audio: ${data?.inputAudio || 'Unknown'}${EOL}`); - logStream.write(` Output Audio: ${data?.outputAudio || 'Unknown'}${EOL}`); - logStream.write(` File Size: ${data?.inputSize / (1024 * 1024) || '0.0'} MiB${EOL}`); - logStream.write(`---------------------------------------------${EOL}`); - logStream.end(EOL); + logStream.on('finish', () => resolve()); + logStream.on('error', (err) => { + if (!logStream.destroyed) logStream.destroy(); + reject(err); + }); + }); } @@ -403,17 +409,18 @@ async function convertAudio(inFile, options = defaultOptions) { // Handlers ffmpegChain .on('error', (err) => { - if (!quiet) { - process.stdout.write('\n'); - writeErrorLog(createLogFile('audioConvError'), { - inputFile: inFile, - outputFile: outFile, - inputSize: fs.statSync(inFile).size - }, err); - log.error('Failed to convert the audio file'); - console.error('Caused by:', err.message); - } - reject(err); + quiet || process.stdout.write('\n'); + writeErrorLog(createLogFile('audioConvError'), { + inputFile: inFile, + outputFile: outFile, + inputSize: fs.statSync(inFile).size + }, err).then(() => { + if (!quiet) { + log.error('Failed to convert the audio file'); + console.error('Caused by:', err.message); + } + reject(err); + }).catch((errLog) => reject(new Error(errLog.message, { cause: err }))); }) .on('progress', (info) => { // Write the progress information to the console