Node-mysql2: Promise error catching.

Created on 27 Feb 2017  路  4Comments  路  Source: sidorares/node-mysql2

Consider the following code:

    AddUser : async (username, email) => {
        let ans = await db.execute(
            "INSERT `users` (`username`, `email`) VALUES (?, ?)",
            [username, email]
            ).catch(ReportError);

        return ans;
    }

I'm trying to make an insert statement with unique fields, in this case it's username and email.

The execute() call returns either an object (if the fields are unique) or a function (which when executed gives the error) when awaited.

It is my impression that the .catch() clause (ReportError) should have been called if there was an error in the query.

This is not a big deal, I could add an if statement. But I'd like to know:

Is this a design decision, an oversight in the promise wrapper implementation or am I not familiar with this convention?

Most helpful comment

so I just tried this example and it worked as expected:

const mysql = require('mysql2/promise')

const func1 = async (username, email) => {
  let ans = 'oops'
  try {
    const db = await mysql.createConnection({database: 'test', user: 'root', port: 33306})
    ans = await db.execute("INSERT `users` (`username`, `email`) VALUES (?, ?)", [username, email])
  } catch(e) {
    console.log('here1')
    console.log(e)
    console.log('here2')
  }
  return ans;
}


func1('username', 'email').then( ans => console.log(ans) )

exception was caught ( console.log(e) line ) and "return ans" executed with initial "oops" value. Also when I forgot to set port initially promise rejected from .createConnection was caught as exception at the same line

All 4 comments

as far as I know rejected promise is thrown as normal sync exception when you use await, so you just wrap your code with try/catch. Let me test this quickly

so I just tried this example and it worked as expected:

const mysql = require('mysql2/promise')

const func1 = async (username, email) => {
  let ans = 'oops'
  try {
    const db = await mysql.createConnection({database: 'test', user: 'root', port: 33306})
    ans = await db.execute("INSERT `users` (`username`, `email`) VALUES (?, ?)", [username, email])
  } catch(e) {
    console.log('here1')
    console.log(e)
    console.log('here2')
  }
  return ans;
}


func1('username', 'email').then( ans => console.log(ans) )

exception was caught ( console.log(e) line ) and "return ans" executed with initial "oops" value. Also when I forgot to set port initially promise rejected from .createConnection was caught as exception at the same line

Yes, I can confirm this works. Please excuse me, I should have posted the ReportError function as well it was my own issue.

function ReportError(msg){
    let newErr = new Error(msg); // placed here to get correct stack
    return err => {
        if(throwException){
            newErr.originalError = err;
            throw newErr;
        }
        console.error(err.stack);
    };
}

This is an error resolution generator, not an error resolution itself. I had to call .catch(ReportError("AddUser")); Sorry about this.

What confused me was that the function was returned as the await's value. I'm terribly sorry.

no worries @MHDante , glad you solved that

Was this page helpful?
0 / 5 - 0 ratings