Just handle error with the associated events:
// If the connection throws an error
connection.on('error',function (err) {
// Do something here
});
// When the connection is disconnected
connection.on('disconnected', function () {
// Do something else here
});
And, of course, when running a query, it's very recommended to always check err
parameter of the callback function before going any further.