Nested try catch issue in JS


Lets say there are three promises

async function a() {
  try{
    await fakePromiseA()
  } catch(e){ 
    rollbackA()
    throw new Error(e?.message)
  } 
}
            

async function b() {
  try{
    await fakePromiseA()
  } catch(e){ 
    // unhandled error
  } 
}
            

async function c() {
  try {
  
  await a();
  await b();

  } catch(e) {
    rollbackC()
    giveFeedbackAndLog()
  }
}
            

Now in this case if a fails the functionality halts and appropriate error are logged but not the same when b fails the flow does not break and it seems like everything worked well.

Solutions

  1. always rollback transactions
  2. throw error if the try catch is nested and required to a flow blocking promise
  3. at least log the error

some additonal info https://softwareengineering.stackexchange.com/questions/118788/is-using-nested-try-catch-blocks-an-anti-pattern

That's pretty much it!