What to do with a 504 Bad Gateway response from 3rd party API using Javascript?

I have a function that calls a 3rd party API (via the function checkPosition) which sometimes fails after a 30 second timeout.

I have the function in a try/catch block, but the 504 crashes the program.

try {
  // Check if position is still open
  if (!(await checkPosition(client, item.dealID))) {
    // If position has been manually closed
    console.log(`Position with period ${key} has been manually removed.`);
    continue;
  }
 
  await closePosition(client, item.epic, item.purchaseAmount, direction);
}
catch (err)
{
  console.error(err);
  res.redirect("/");
}

I tried to catch the 504 but this is not working for me.