I'm have a recursive function which is used to get SQL files from a CodeCommit repo on AWS and run them in sequence. We need to wait for the previous file to complete before running the next one. If one of the SQL files fails, I need the catch block to return information on the file which failed.
What I'm seeing with my code at the moment is that the catch block is repeating once for each SQL file in the repo. From my understanding, the 'throw' statement should return to the catch block of the function that initially called the function. Could anyone point out what I'm doing wrong here?
const getFileData = async (newSQLFiles,processed=[]) => {
try{
if(newSQLFiles.length ===0){
client.release();
await pool.end().then(() => console.log('DB Connection pool closed.'))
return processed;
}
var params = {
filePath: newSQLFiles[0].relativePath,
repositoryName: 'testDBScripts' //Use environment variable
};
const data = await codecommit.getFile(params).promise();
await runScripts(data);
processed.push(newSQLFiles[0].relativePath)
}catch(err){
console.log(err)
throw [err,processed];
}
return await getFileData(newSQLFiles.slice(1),processed);
}
await getFileData(newSQLFiles)
.then(processed=>console.log("Following products are updated.",processed))
.catch(async ([e, file])=> {
client.release();
await pool.end().then(() => console.log('DB Connection pool closed.'))
//await codePipelineJobFailed("SQL file " + file + " failed with : " + e)
throw new Error("SQL file " + file + " failed with : " + e)}
)