How does node.js connect to the database with catch exception?

I use mysql of node connection

// 
addUser(userName, pwd) {
    let connection = Mysql.createConnection({
        host: MYSQL_CONFIG.HOST,
        user: MYSQL_CONFIG.USER,
        password: MYSQL_CONFIG.PASSWORD,
        database: MYSQL_CONFIG.DATABASE
    });

    let sql = "INSERT INTO user(name, pwd) VALUES(?, ?)";
    let sqlParams = [userName, pwd];

    return new Promise((resolve) => {
        connection.query(sql, sqlParams, function (err, result) {
            if (err) {
                resolve(": " + err.message);
                connection.end();

            }

            resolve(``);
            connection.end();

        });
    })

}

if the pwd exceeds the length limit of the pwd in the database, an exception will be thrown on the console

clipboard.png

sometimes it means that the length of the field is exceeded, but I can"t find it.
both of these exceptions can cause program interruptions.

how can I change it to catch an exception so that the program will not be interrupted?

Oct.06,2021

you can use the try catch statement, or use the reject parameter of promiese or the .catch () method . Which one has been used is determined according to your business and code style.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3eb5b-2b980.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3eb5b-2b980.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?