Skip to content

Commit

Permalink
fix(audioconv): Ensure error logging works on quiet enabled
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
mitsuki31 committed Aug 24, 2024
1 parent 92daed6 commit 84a4735
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions lib/audioconv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]<ACONV> ${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]<ACONV> ${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);
});
});
}


Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 84a4735

Please sign in to comment.