Search code examples
javascriptelectronsyntax-error

Javascript syntax errors swallowed and hidden in async/await functions


I'm learning Electron.js and I want to use async/await feature in my code but I'm a bit disappointed because syntax errors are swallowed and silent which make my future projects a nightmare for debugging.

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


Solution

  • 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