jeremydaly/serverless-mysql

Errors are inconsistent and don't match those thrown by mysql.js

Opened this issue · 1 comments

Good afternoon, I hope this isn't a duplicate.

While attempting to debug an error, we noticed that the errors being thrown by the query() were not providing us with the information we expected.

To illustrate, I've produced an ER_ACCESS_DENIED_ERROR error locally with both serverless-mysql and mysql.js.

// ++ mysql.js ++
const connection = mysql.createConnection(databaseDetails);
connection.query("SELECT 1", function (error, results, fields) {
    if (error) {
        console.log(error);
    }
});

Outputs:

console.log
    Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES)
        // ++ Stack trace omitted++
    {
      code: 'ER_ACCESS_DENIED_ERROR',
      errno: 1045,
      sqlMessage: "Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES)",
      sqlState: '28000',
      fatal: true
    }

By contrast;

// ++ serverless-mysql ++
const connection = serverlessMysql({
    library: mysql,
    config: databaseDetails,
});

try {
    await connection.query("SELECT 1");
    await this.connection.end();
} catch (error) {
    console.log(error);
}

Outputs;

console.log
    Error: Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'xxx.xxx.xxx.xxx' (using password: YES)
        // ++ Stack trace omitted ++

You can see the original Error thrown by mysql.js appears to have been stringified, and then re-thrown within another Error by serverless-mysql, losing the helpful MysqlError object that can be interrogated programmatically.

Is this a bug? How can we get the code, errno, etc. out of the error thrown by serverless-mysql?

I forgot to mention an important factor in this.

I can see in our application logs that some errors thrown by serverless-mysql do seem to include the whole MysqlError object - specifically when the error code in question is ECONNRESET however I don't know how to reproduce that locally.