Error: Connection lost: The server closed the connection.
I know it’s when the server closes the connection - I am fine with the connection closing. How to I caught this error so it doesn’t throw the error in the Log?
The error you are experiencing is “uncaught,” and can be fixed with a quick .catch(e => console.log(e)) at the end of the line. I use this with my discord bot when, say, it sends an empty message.
However, in await/async functions catching may be different as seen in the link above. I’m not sure about the structure of your project, so I’m not sure if just using .catch will work.
I hope this helped, and happy coding!
OK I took a look. And I am not having issues with unhandled errors when connecting or running SQL. Using a .catch with this would caught if the error occurs on the connect or sql run. This is when the server is basically timing out the connection. How do I handle this gloabally?
The server disconnecting me if fine. I just want to stop the RED error from showing up in the LOG. How do I stop that from happening? The disconnection is fine.
Hm…you are right .catch only works in running errors. Perhaps you could put a catch at the very end… Unfortunately I do not have much experience with SQL – I can’t really help you. Sorry! I hope someone can help you.
Hey Cori thanks. I could share the project but not sure if you could see the code since I have it locked? Rather not be open to the public.
try {
await con.connect(async function(err) {
if (err) throw err;
await con.query(qrySelect, async function (err, rows, fields) {
if (err) throw err;
if (rows.length > 0) {
_.each(rows, async function(row) {
So you can see that on the con.connect I do check err but that would be on the open of the connection. the error I am getting is a timeout I am sure - again which is fine - I would just like to caught the error.
Hey @Jeff it looks to me like the mysql.connection object sends events on error, so I’m thinking you could add an error event handler after your connection definition and handle the “error” appropriately. https://www.npmjs.com/package/mysql#error-handling hints at this; you might try adding something like