0

Im trying to keep a record from all my errors in a WebService I'm making in node-js;

I've written the following code to keep track of a mysql query possible error:

        var err_db = [];

    try{
        if(error.length == 0){
            ...
            var con = mysql.createConnection({
              host: "my_host",
              user: "my_user",
              password: "my_pass",
              database: "my_db"
            });

            con.connect(function(err) {
              if (err) err_db[err_db.length] = err.message;
              con.query("IM TRYING HARD TO GET AN SQL ERROR", function (err) {
                if (err) err_db[err_db.length] = err.message;
                console.log(err_db); //FIRST LOG SHOWS CORRECT
              });
              console.log(err_db); // THE ERROR DISAPEARS FROM ARRAY
            });
        }
    }
    catch(err){
        if(err) err_db[err_db.length] = err.message;
    }

The problem is the error only keeps stored in array inside the con.query function, after that it disappear, and I want to keep it in a array because later on I intend in sending this possible errors as a JSON to through the WebService response. Thanks in advance.

2 Answers 2

2

This is a normal asynchronous nature of node.js. Since the query is executed in a slight greater time so next line is executed first.

try{
        if(error.length == 0){
            ...
            var con = mysql.createConnection({
              host: "my_host",
              user: "my_user",
              password: "my_pass",
              database: "my_db"
            });

            con.connect(function(err) {
              if (err) err_db[err_db.length] = err.message;
              con.query("IM TRYING HARD TO GET AN SQL ERROR", function (err) {
                if (err) err_db[err_db.length] = err.message;
                console.log(err_db); //FIRST LOG SHOWS CORRECT
               // throw the error from here 
              });
              console.log(err_db); // THIS EXECUTED EARLIER THAN THE PREVIOUS
            });
        }
    }
    catch(err){
        if(err) err_db[err_db.length] = err.message;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the help!
1

Asynchronous code cannot catch exceptions using try-catch.

You can try the following code.

var EventEmitter = require('events');
var emitter = new EventEmitter();
var err_db = [];

var con = mysql.createConnection({
  host: "my_host",
  user: "my_user",
  password: "my_pass",
  database: "my_db"
});

if (error.length == 0) {
  con.connect(function (err) {
    if (err) {
      emitter.emit('err_db', err);
      return;
    }
    con.query("IM TRYING HARD TO GET AN SQL ERROR", function (err) {
      // if (err) err_db[err_db.length] = err.message;
      // console.log(err_db); //FIRST LOG SHOWS CORRECT
      if (err) {
        emitter.emit('err_db', err);
        return;
      }
    });
    console.log(err_db); // THE ERROR DISAPEARS FROM ARRAY
  });
}

emitter.on('err_db', (err) => {
  // handle db err...
  err_db[err_db.length] = err.message
});

1 Comment

Thanks a lot! Didn't realize this was an asynchronous function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.