Why Does This Mysql Error Causes Nodejs To Crash Instead Of Going To The Catch Function?
Solution 1:
Actually, @Quentine was close to the right thing...
It is "sort of" a bug in mysql2
,
i use sort-of because https://github.com/sidorares/node-mysql2/issues/902 suggests the development team of mysql2
is o.k. with it.
it is an issue with the way mysql2.pool passes the call to the created connection, which does not pass the exception to the wrapping promise.
I ended up making my own wrapping function to create the connection + call execute wrapped in proper promise handling.
import mysql = require('mysql2');
private async queryDB(query:string, useExecute: boolean = false, ...args:any[]) : Promise<any[]>
{
return new Promise<any[]>((resolve, reject)=>{
for(var i = 0; i < args.length; ++i)
{
if(args[i]===undefined)
args[i] = null;
}
this.dbPool.getConnection((err, conn)=>{
if(err){
reject(err);
return;
}
let cb = function(err: mysql.QueryError, results: any[], fields: mysql.FieldPacket[]) {
conn.release();
if(err)
{
reject(err);
return;
}
resolve(results);
}
if(useExecute)
conn.execute(query, args, cb);
else
conn.query(query, args, cb);
});
});
}
Solution 2:
mysqlConnectionPool.execute
is throwing the exception before creating a promise.
i.e. the exception is not thrown from within the promise.
To catch it you would need to try {} catch (e) {}
around the call to mysqlConnectionPool.execute
.
Solution 3:
Well,
I'm guessing that you are using the standard mysql package which it seems not supporting Promises, instead, it accepts a standard node callback function(err, results, fields) {}
as an argument of the execute method.
So since you haven't defined a valid callback the script will just throw an exception.
Post a Comment for "Why Does This Mysql Error Causes Nodejs To Crash Instead Of Going To The Catch Function?"