简体   繁体   中英

Javascript syntax errors swallowed and hidden in async/await functions

I'm learining electron.js and I want to use aync/await feature in my code but I'm a bit diapointed because syntax errors are swallowed and silent which make my future projects a nightmare for debuging.

db module:

exports.connect = function(){
    return new Promise( (resolve, reject) => {
        connection = mysql.createConnection({
             host     : host,
             port     : port,
             user     : user,
             password : null, // or the original password : 'apaswword'
             database : database
        });

        query = util.promisify(connection.query).bind(connection);

        connection.connect(function(error) {
             // in case of error
             if(error){
                 reject(error);
             }

             resolve(true);
        });

        connection.on('error', error => {
            dispatcher.send('connection-error', error.code);
        });
    });
}

bootstrap module:

async function connectDB(){
    try{
        let connected = await db.connect(THIS_SHOULD_THROW_ERROR);

        return connected;
    }catch( error ){
        dispatcher.send('connection-error', error);
    }
}

exports.init = async function( win ){

    dispatcher.init(win);

    try{
        const connected = await connectDB();

        /*if(!connected){
            dispatcher.send('fatal-error', "MYSQL NOT CONNECTED");
        }*/
    }catch( error ){
        dispatcher.send('fatal-error', error);
    }
}

This code is trying to connect to mysql and send error if it can't connect, but notice the syntax error "THIS_SHOULD_THROW_ERROR" that should halt execution or throw error, but it doesn't and my code has no errors at all even if it can't connect to mysql.

Notice that if I remove syntax error my code works well and catches mysql connection error.

I've read everywhere that is normal behavior of javascript async/promises code, but I'd like to know if there is a solution to catch syntax errors to make my debuging easier. Thank you

If you have a syntax error inside a try/catch block or you are using a catch all mechanism (ie process.on('uncaughtException'... ) the syntax error would be swallowed:

/* content of test.js */

console.log('hello')
THIS_SHOULD_THROW_ERROR // comment this line and run again

try {
  THIS_SHOULD_THROW_ERROR_BUT_DOESNOT
} catch (err) {
  // using err will throw exception: console.log(err)
  console.log('error happened')
}

Now run the script with and without comment in the line specified:

$ node test.js

So you are doing such somewhere in your code.

PS:

async function connectDB(){
    try{
        let connected = await db.connect(THIS_SHOULD_THROW_ERROR);

        return connected;
    }catch( error ){
        dispatcher.send('connection-error', error);
        // throw error
    }
}

The db.connect(THIS_SHOULD_THROW_ERROR) is in try block while you don't throw the error. If dispatcher.send doesn't throw the error in some point that error is swallowed.

Thanks to Xarqron & Bergi I made it finnaly work, I just had to throw error in both connectDB and init catch's

async function connectDB(){
    try{
        let connected = await db.connect(THIS_SHOULD_THROW_ERROR);

        return connected;
    }catch( error ){
        dispatcher.send('connection-error', error);
        throw error;
    }
}

exports.init = async function( win ){
    dispatcher.init(win);

    try{
        const connected = await connectDB();

        /*if(!connected){
            dispatcher.send('fatal-error', "MYSQL NOT CONNECTED");
        }*/
    }catch( error ){
        dispatcher.send('fatal-error', error);
        throw error;
    }
}

Do you advice to always throw error in every catch block so that debugging would be easier ? Because I had hard time to find this kind of bug without any console warning or error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM