sidorares / node-mysql2

:zap: fast mysqljs/mysql compatible mysql driver for node.js
https://sidorares.github.io/node-mysql2/
MIT License
4.06k stars 615 forks source link

Long stack trace missing from errors #1067

Open abney317 opened 4 years ago

abney317 commented 4 years ago

I am unable to see the long version of the stack traces from errors.

Error: Unknown column 'test' in 'field list'
    at Packet.asError (C:\example\node_modules\mysql2\lib\packets\packet.js:712:17)
    at Query.execute (C:\example\node_modules\mysql2\lib\commands\command.js:28:26)
    at PoolConnection.handlePacket (C:\example\node_modules\mysql2\lib\connection.js:408:32)
    at PacketParser.onPacket (C:\example\node_modules\mysql2\lib\connection.js:70:12)
    at PacketParser.executeStart (C:\example\node_modules\mysql2\lib\packet_parser.js:75:16)
    at Socket.<anonymous> (C:\example\node_modules\mysql2\lib\connection.js:77:25)
    at Socket.emit (events.js:200:13)
    at Socket.EventEmitter.emit (domain.js:471:20)
    at addChunk (_stream_readable.js:290:12)
    at readableAddChunk (_stream_readable.js:271:11) {
  code: 'ER_BAD_FIELD_ERROR',
  errno: 1054,
  sqlState: '42S22',
  sqlMessage: "Unknown column 'test' in 'field list'"
}

Becomes a bit difficult to debug things without any of my own code referenced in the stack trace.

Node MySQL has the long stack traces. Is there a way to enable this, am I missing something, or has it just not been implemented here for MySQL2?

sidorares commented 4 years ago

No, it's just not implemented ( here is how mysqljs is doing it ). The reason was partially performance ( having new Error() anywhere in the code used to prevent v8 from some optimisations even if it's behind the flag ), it's much better now so I might consider adding

You can do it yourself in your code by adding 3rd party long stack trace library such as https://github.com/mattinsler/longjohn

With async/await it's handled automatically by v8, so promise wrapper has much better stacks

jpike88 commented 3 years ago

it's frustrating having to hunt around because the stack trace is useless, could you please consider making this a priority if it's easy to do

jpike88 commented 3 years ago

And can just hide behind a flag if you're so worried about performance?

samuel-soubeyran commented 2 years ago

Came here while troubleshooting similar issues in a codebase using knex+mysql2. I don't think there is a way to make knex use the promise wrapper.

RedSpid3r commented 1 year ago

Just came here to bump this because having useless stack traces is exceedingly frustrating when trying to debug. Is there any ETA on when this might be looked at/considered for implementation? If not I may have to revert to using mysqljs because it's just not feasible to be unable to track down issues when they are impacting production environment

K1Z4 commented 1 year ago

I had this issue because I was wrapping con.query in my own promise wrapper.

Once I updated my code to use 'mysql2/promise' the stack traces were helpful again.

Error: Unknown column 'invalid' in 'field list'
    at PromisePoolConnection.query (/home/k1z4/Utils/node_modules/mysql2/promise.js:94:22)
    at FriendRepository.query (file:///home/k1z4/Utils/src/mysqlBase.js:6:21)
    at FriendRepository.insert (file:///home/k1z4/Utils/src/mysqlBase.js:72:27)
    at file:///home/k1z4/Utils/test/testApp.js:43:28
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async FriendRepository.inTransaction (file:///home/k1z4/Utils/src/mysqlBase.js:105:32)
    at async file:///home/k1z4/Utils/test/testApp.js:76:1 {
  code: 'ER_BAD_FIELD_ERROR',
  errno: 1054,
  sql: "INSERT INTO friend SET `name` = 'test4', `invalid` = 'invalid'",
  sqlState: '42S22',
  sqlMessage: "Unknown column 'invalid' in 'field list'"
}

My new query func with useful stack traces:

   static query(sqlString, params, pool = getPool()) {
        return pool.query(sqlString, params).then(([rows, fields]) => rows)
    }

My old query func with bad stack traces:

    static query(sqlString, params, con = getPool()) {
        if (typeof (sqlString) !== 'string') throw new Error("sqlString must be a string");
        if (!typeof params == 'object') throw new Error("params must be an object");

        return new Promise((resolve, reject) => {
            con.query(sqlString, params, (error, results, fields) => {
                if (error) {
                    return reject(error);
                }

                resolve(results, fields);
            });
        });
    }
nolimitdev commented 1 year ago

Yes, I have just found this bug, that after migrating from npm/mysql to npm/mysql2 traces are lost. Option trace : true/false described in mysql lib https://github.com/mysqljs/mysql#connection-options is not working in this mysql2 lib. Unfortunately we can not migrate to mysql2 due to this bug.

UPDATE: It should be added here: https://github.com/sidorares/node-mysql2/tree/master/documentation/en#known-incompatibilities-with-node-mysql that trace option not working/not supported/not implemented.