I have a node.js script that runs a MySQL query. When error is encountered with this query, the node.js process dies. It uses the mysql-node module. https://github.com/mysqljs/mysql
Here is my code;
function runQuery() {
return new Promise(function (resolve, reject) {
try {
var connection = test_mysql.create_MySQL_conn();
var query_str =
//MySQL query. blah blah
;
var query_param = [];
connection.query(query_str, query_param, function (err, rows, fields) {
if (err) {
console.log(err);
return reject(err);
}
else {
resolve(rows);
}
});
}
catch (err) {
reject(err);
}
});
}
How do I modify it such that the script does not die when error is encountered with the mysql query?
The premise of your question is likely to be wrong. The script may not die even if you encounter error in the MySQL query. You can do a simple test.
You can run the code below.
function Delay(duration) {
return new Promise((resolve) => {
setTimeout(() => resolve(), duration);
});
}
function test_func() {
Promise.resolve()
.then(() => runQuery() );
}
Promise.resolve()
.then(() => Delay(3000))
.then(() => test_func() )
.then(() => Delay(3000))
.then(() => test_func() );
If test_func()
runs 2 times, it means the script did not die. Probably, you encountered a long error message and thought the script died when it actually did not.