why does this mysql error causes nodejs to crash instead of going to the catch function?

后端 未结 3 1915
轻奢々
轻奢々 2021-01-18 11:10

I have a mysql statement that creates an entry, it has a .then function and a .catch function, but when the following error occurs:

T

3条回答
  •  隐瞒了意图╮
    2021-01-18 12:04

    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
        {
            return new Promise((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);                
                });
            });
        }
    

提交回复
热议问题