Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Handling? #2992

Open
kingram6865 opened this issue Aug 27, 2024 · 5 comments
Open

Error Handling? #2992

kingram6865 opened this issue Aug 27, 2024 · 5 comments
Labels

Comments

@kingram6865
Copy link

kingram6865 commented Aug 27, 2024

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
Copy link
Sponsor Collaborator

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
Copy link
Author

kingram6865 commented Aug 27, 2024

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
Copy link
Author

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

@wellwelwel
Copy link
Sponsor Collaborator

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants