Error: Connection lost: The server closed the connection

So I am using MYSQL NPM. After using the MYSQL connection the project ends. I know I am not closing the connection and I get this error

events.js:183
throw er; // Unhandled ‘error’ event

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?

Hey Jeff! I believe this link might help you:


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?

Here is the error in the Glitch projectconnection%20error

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 @Jeff can you share anything else about your project, like the code around where you manage the MySQL connection, or your project name?

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.

If you provide your project name, myself and a few other Glitch staff will be able to see it.

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

connection.on('error', function(err) {
  console.log(err.code);
});

at https://glitch.com/edit/#!/ember-mastodon?path=server.js:105:0, for instance.

Let us know how that goes!

1 Like

Thanks Cori I will try that and let you know

1 Like

BINGO!!! Thank you so much Cori! That worked.

1 Like

A post was split to a new topic: Connection is not defined