sidorares / node-mysql2

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

Error Handling? #2992

Open kingram6865 opened 2 weeks ago

kingram6865 commented 2 weeks ago

I have a REST API and for an INSERT handler I have a custom (45000) error in MySQL. Node throws the stack in the console but I cannot capture that info in my try catch for proper handling.

There's no documentation on Error handling for mysql2. Any thoughts/suggestions?

Error: DUPLICATE: Already have that data
    at PromisePool.query (/home/kingram/PROJECTS/omnibusapi/node_modules/mysql2/promise.js:356:22)
    at call (/home/kingram/PROJECTS/omnibusapi/controllers/data.js:8:36)
    at tryCatch (/home/kingram/PROJECTS/omnibusapi/controllers/data.js:2:1)
    at Generator._invoke (/home/kingram/PROJECTS/omnibusapi/controllers/data.js:2:1)
    at Generator.next (/home/kingram/PROJECTS/omnibusapi/controllers/data.js:2:1)
    at asyncGeneratorStep (/home/kingram/PROJECTS/omnibusapi/controllers/data.js:2:1)
    at _next (/home/kingram/PROJECTS/omnibusapi/controllers/data.js:2:1)
    at /home/kingram/PROJECTS/omnibusapi/controllers/data.js:2:1
    at new Promise (<anonymous>)
    at apply (/home/kingram/PROJECTS/omnibusapi/controllers/data.js:2:1)
    at apply (/home/kingram/PROJECTS/omnibusapi/controllers/data.js:14:2)
    at executeSQL (/home/kingram/PROJECTS/omnibusapi/controllers/data.js:5:26)
    at call (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:26:36)
    at tryCatch (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at Generator._invoke (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at Generator.next (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at asyncGeneratorStep (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at _next (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at /home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1
    at new Promise (<anonymous>)
    at apply (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at handler (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:37:4)
    at Layer.handle [as handle_request] (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/route.js:149:13)
    at Route.dispatch (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/route.js:119:3)
    at Layer.handle [as handle_request] (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/layer.js:95:5)
    at /home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/index.js:284:15
    at Function.process_params (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/index.js:346:12)
    at next (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/index.js:280:10)
    at serveStatic (/home/kingram/PROJECTS/omnibusapi/node_modules/serve-static/index.js:75:16)
    at Layer.handle [as handle_request] (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/index.js:328:13)
    at /home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/index.js:346:12)
    at next (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/index.js:280:10)
        at serveStatic (/home/kingram/PROJECTS/omnibusapi/node_modules/serve-static/index.js:75:16)
    at Layer.handle [as handle_request] (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/index.js:328:13)
    at /home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/index.js:346:12)
    at next (/home/kingram/PROJECTS/omnibusapi/node_modules/express/lib/router/index.js:280:10)
    at /home/kingram/PROJECTS/omnibusapi/node_modules/body-parser/lib/read.js:137:5
    at AsyncResource.runInAsyncScope (node:async_hooks:206:9)
    at invokeCallback (/home/kingram/PROJECTS/omnibusapi/node_modules/raw-body/index.js:238:16)
    at done (/home/kingram/PROJECTS/omnibusapi/node_modules/raw-body/index.js:227:7)
    at IncomingMessage.onEnd (/home/kingram/PROJECTS/omnibusapi/node_modules/raw-body/index.js:287:7)
    at IncomingMessage.emit (node:events:518:28)
    at IncomingMessage.emit (node:domain:488:12)
    at endReadableNT (node:internal/streams/readable:1696:12)
    at processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'ER_SIGNAL_EXCEPTION',
  errno: 1644,
  sql: "INSERT INTO a (data) VALUES (data)",
  sqlState: '45000',
  sqlMessage: 'DUPLICATE: Already have that data'
}

console.log(err) produces:

TypeError: Invalid attempt to destructure non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
    at _nonIterableRest (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at _slicedToArray (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at call (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:26:51)
    at tryCatch (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at Generator._invoke (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at Generator.next (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at asyncGeneratorStep (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at _next (/home/kingram/PROJECTS/omnibusapi/routes/data/addNewData.js:2:1)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
wellwelwel commented 2 weeks ago

Hi @kingram6865, could you provide some code context?

There's no documentation on Error handling for mysql2.

See the How to handle errors? FAQ section 🙋🏻‍♂️

kingram6865 commented 2 weeks ago

Hi @kingram6865, could you provide some code context?

There's no documentation on Error handling for mysql2.

See the How to handle errors? FAQ section 🙋🏻‍♂️

Yeah, the FAQ does not add any new or detailed information.

I included the output of error handling, which is the same as that offered in the FAQ. The node error handler throws the stack, but I cannot capture the output from MySQL. How do I capture that error info? It is not provided by console.log(err)

What kind of code context do you want? It's a simple try-catch block.

try {
      const [rows, fields] = await executeSQL(sql)
      res.json(rows)
    } catch(err) {
      // console.log(Object.keys(err))
      // console.log(err.name, err.message)
      // throw err;
      // res.json(err)
      // res.send(err.message)
      // for (let k in err) { console.log(`Line 34: ${k}: ${error[k]}`) }
      if (err instanceof Error) {
        console.log('execute error:', err);
      }
    }

I'm preventing duplicate entries in the table at the MySQL server:

IF (v_hashdupe = 0 && NEW.data IS NOT NULL) THEN
  SET NEW.hash = v_hash;
ELSEIF (v_hashdupe = 0 && NEW.data IS NULL) THEN
  signal sqlstate '45000' set message_text = "MISSING: No hash data";
ELSEIF (v_hashdupe > 0) THEN
  signal sqlstate '45000' set message_text = "DUPLICATE: Already have that data";
END IF;
kingram6865 commented 6 days ago

I provided details two weeks ago. Is anyone going to respond?

wellwelwel commented 6 days ago

I provided details two weeks ago. Is anyone going to respond?

I know I don't need to explain myself, but I really like how the open-source community works.

I usually create a priority queue based on how many users are affected by a bug/issue. For example a single PR (#2988) that fixes 8 issues and resolves a large discussion all at once, with practically zero code changes, also opened 2 weeks ago and no feedbacks and it's totally fine. It needs investigation, make sure nothing gets broken, etc (these things demand time and effort of the community and maintainers).

At a point where the JavaScript error stack is not sufficient for your case or doesn't return a specific behavior, I don't know what should be returned. That's why we keep unanswered issues open, so any user in the community who has experienced and solved it can comment 🤝

kingram6865 commented 5 days ago

I provided details two weeks ago. Is anyone going to respond?

I know I don't need to explain myself, but I really like how the open-source community works.

I usually create a priority queue based on how many users are affected by a bug/issue. For example a single PR (#2988) that fixes 8 issues and resolves a large discussion all at once, with practically zero code changes, also opened 2 weeks ago and no feedbacks and it's totally fine. It needs investigation, make sure nothing gets broken, etc (these things demand time and effort of the community and maintainers).

At a point where the JavaScript error stack is not sufficient for your case or doesn't return a specific behavior, I don't know what should be returned. That's why we keep unanswered issues open, so any user in the community who has experienced and solved it can comment 🤝

Ok. Would be nice to find an answer at some point. If there is one.