-1

I am purposefully causing the connection.execute aspect of this promise to fail. When it does fail I get a 'Error Occured' in the console followed by an uncaught exception, the error I am producing, and then Node completely shutting down.

async function procExecute(query, binds, clientdetails = [], procdetails) {
  return new Promise((resolve, reject) => {
      // Acquire connection from pool
      myPool.acquire().then(connection => {
          // Execute the query
          connection.execute({
              sqlText: query,
              binds: binds,
              parameters: { MULTI_STATEMENT_COUNT: procdetails.statementcount },
              complete: (err, stmt, rows) => {
                  console.log(`Conn: ${connection.getId()} fetched ${rows && rows.length} rows`);
                  // Return result
                  console.log(JSON.stringify(rows))
                  if (err) {
                    console.log('Error Occurred: ' + err)
                    // Return Error
                    resolve([{
                      "ERROR_CODE": Number(err.data.errorCode),
                      "ERROR_MSG": err.message
                    }])
                  //  reject(new Error(err.message))
                  } else {
                    resolve(
                      [{
                        "ERROR_CODE": rows[0].P_ERROR_CODE,
                        "ERROR_MSG": rows[0].P_MSG
                      }]
                    );
                  }
                  // Return connection back to pool
                  myPool.release(connection);
              }
          });
        }).catch(
          console.log('Error Occured'),
          (err) => {
            reject([{
              "ERROR_CODE": Number(9999),
              "ERROR_MSG": err
            }])
          } 
)
    });

}

module.exports.procExecute = procExecute;
user68288
  • 702
  • 2
  • 6
  • 27
  • Please don't return a new promise inside an async function. It's redundant. – evolutionxbox Aug 10 '23 at 15:13
  • The issue lies outside of your try/catch since you are rejecting. Wrap your call of `procExecute` in a try/catch and check the error. – cyberwombat Aug 10 '23 at 15:13
  • 3
    There are a **lot** of mistakes in this code: you never use `await` making the `async` pointless. Use of the [explicit Promise constructor anti-pattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it). Mixing Promise code with callback code. Since `pool.acquire` returns a Promise, `connection.execute` almost certainly does too and you aren't `.catch`ing it. – Jared Smith Aug 10 '23 at 15:15

1 Answers1

-1

i think the catch block in your code is not properly handling the error. You're logging 'Error Occurred' before defining the error function, and this is not how you should structure your catch block.

Here's a corrected version:

async function procExecute(query, binds, clientdetails = [], procdetails) {
  return new Promise((resolve, reject) => {
    // Acquire connection from pool
    myPool.acquire().then(connection => {
      // Execute the query
      connection.execute({
        sqlText: query,
        binds: binds,
        parameters: { MULTI_STATEMENT_COUNT: procdetails.statementcount },
        complete: (err, stmt, rows) => {
          console.log(`Conn: ${connection.getId()} fetched ${rows && rows.length} rows`);
          // Return result
          console.log(JSON.stringify(rows));
          if (err) {
            console.log('Error Occurred: ' + err);
            // Return Error
            resolve([{
              "ERROR_CODE": Number(err.data.errorCode),
              "ERROR_MSG": err.message
            }]);
          } else {
            resolve(
              [{
                "ERROR_CODE": rows[0].P_ERROR_CODE,
                "ERROR_MSG": rows[0].P_MSG
              }]
            );
          }
          // Return connection back to pool
          myPool.release(connection);
        }
      });
    }).catch(err => {
        console.log('Error Occurred:', err);
        reject([{
          "ERROR_CODE": Number(9999),
          "ERROR_MSG": err
        }]);
      });
  });
}

module.exports.procExecute = procExecute;
MaikeruDev
  • 1,075
  • 1
  • 19