Home > Software engineering >  Code still continues even after an error is thrown
Code still continues even after an error is thrown

Time:12-04

I am new to Node.js and Express.js, and would like some guidance.

I am trying to do an insertion to MySQL database where there are tables named delivery_order and delivery_order_item. When the user submitted the delivery order information (this goes into the former table) including the delivered goods (the goods will be inserted into the latter table where it has a foreign key of delivery_order_number), I would like to do an insertion into both tables. However, if an error is encountered during the insertion into delivery_order table, the goods will also not be inserted into the goods table, hence the beginTransaction.

The problem is for example when I try to insert a duplicate primary key of delivery_order table (which obviously gives me an error), the codes still insert the goods into its table and don't abort the whole process. I made sure to write throw err inside all the error scopes, but it doesn't work.

Any advice would be appreciated.

delivery_order.insert = (data) => {
  const tableData = data.tableData;
  const formInfo = data.formInfo;

  var datetime = new Date(formInfo.date);
  datetime = moment(datetime).format("YYYY-MM-DD");

  var duetime = new Date(formInfo.due);
  duetime = moment(duetime).format("YYYY-MM-DD");

  let formValues = {
    number: formInfo.number,
    name: formInfo.name,
    address: formInfo.address,
    date: datetime,
    due: duetime,
    city: formInfo.city,
    note: formInfo.note,
  };

  let itemValues = [];

  for (var i = 0; i < tableData.length; i  ) {
    itemValues.push([
      formInfo.number,
      tableData[i].code,
      tableData[i].name,
      tableData[i].qty,
      tableData[i].unit,
    ]);
  }

  return new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      if (err) return reject(err);
      connection.beginTransaction((err) => {
        if (err) {
          connection.rollback(() => {
            connection.release();
            throw err;
          });
        }
        connection.query(
          "INSERT INTO delivery_order SET ?",
          formValues,
          (err, results) => {
            if (err) {
              connection.rollback(() => {
                connection.release();
                throw err; // Code still continues down there and doesn't abort even after getting an error here.
              });
            }
            connection.query(
              "INSERT INTO delivery_order_item (delivery_order_number, code, name, qty, unit) VALUES ?",
              [itemValues],
              (err, results) => {
                if (err) {
                  connection.rollback(() => {
                    connection.release();
                    throw err;
                  });
                }

                connection.commit(function (err) {
                  if (err) {
                    connection.rollback(() => {
                      connection.release();
                      throw err;
                    });
                  }

                  connection.release();
                  return resolve("ok");
                });
              }
            );
          }
        );
      });
    });
  });
};

CodePudding user response:

Abort the process if you want a simple way to stop it:

process.exit(1);

Just a quick note on why the errors aren’t actually exiting: When you throw an error, it returns the error to whatever is executing your given function. From this code, it looks like you’re making a method that the MySQL engine executes, which would explain why the process isn’t exiting. MySQL probably handles errors in a completely different way than plain Node.

  • Related