Node-mysql2: How can I catch this connection error

Created on 17 Nov 2017  路  2Comments  路  Source: sidorares/node-mysql2

const mysql = require('mysql2');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'knex_test'
});

connection.query(
  'kill connection_id()',
  function(err, results, fields) {
    console.log('CALLBACK', err);
  }
);

process.on('uncaughtException', function (err) {
  console.log('uncaughtException', err);
})

This simple test program returns

Mikaels-MacBook-Pro-2:knex mikaelle$ node mysql2-test.js 
CALLBACK { Error: Connection was killed
    at Packet.asError (/Users/mikaelle/Projects/Vincit/knex/node_modules/mysql2/lib/packets/packet.js:703:13)
    at Query.Command.execute (/Users/mikaelle/Projects/Vincit/knex/node_modules/mysql2/lib/commands/command.js:28:22)
    at Connection.handlePacket (/Users/mikaelle/Projects/Vincit/knex/node_modules/mysql2/lib/connection.js:515:28)
    at PacketParser.onPacket (/Users/mikaelle/Projects/Vincit/knex/node_modules/mysql2/lib/connection.js:94:16)
    at PacketParser.executeStart (/Users/mikaelle/Projects/Vincit/knex/node_modules/mysql2/lib/packet_parser.js:77:14)
    at Socket.<anonymous> (/Users/mikaelle/Projects/Vincit/knex/node_modules/mysql2/lib/connection.js:102:29)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10) code: undefined, errno: 1927, sqlState: '#70100' }
uncaughtException { Error: Connection lost: The server closed the connection.
    at Socket.<anonymous> (/Users/mikaelle/Projects/Vincit/knex/node_modules/mysql2/lib/connection.js:113:35)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9) fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }

The problem is that there is no way that I can catch the uncaughtException, which means that in this situation my app will be killed... Is there any good way to catch that Socket error? Shouldn't mysql2 driver subscribe and handle that error?

question

Most helpful comment

hi @elhigu
You need to attach error handler to connection object itself ( in fact, it's best practice to always do that )

const mysql = require('mysql2');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'knex_test'
});

connection.on('error', function(err) {
  console.log("I'm dead");
})

connection.query(
  'kill connection_id()',
  function(err, results, fields) {
    console.log('CALLBACK', err);
  }
);

Or you can use connections pool, In that case pool will take care on connection level error handling

All 2 comments

hi @elhigu
You need to attach error handler to connection object itself ( in fact, it's best practice to always do that )

const mysql = require('mysql2');

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'knex_test'
});

connection.on('error', function(err) {
  console.log("I'm dead");
})

connection.query(
  'kill connection_id()',
  function(err, results, fields) {
    console.log('CALLBACK', err);
  }
);

Or you can use connections pool, In that case pool will take care on connection level error handling

Thanks for the response, setting connection.on('error', ...) indeed seems to work fine in this case, though I'm still seeing

    connection.on('error', ...) prints 
{ Error: Connection lost: The server closed the connection.
        at Socket.<anonymous> (/Users/mikaelle/Projects/Vincit/knex/node_modules/mysql2/lib/connection.js:113:35)
        at emitNone (events.js:91:20)
        at Socket.emit (events.js:185:7)
        at endReadableNT (_stream_readable.js:974:12)
        at _combinedTickCallback (internal/process/next_tick.js:80:11)
        at process._tickCallback (internal/process/next_tick.js:104:9) fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
process.on('uncaughtException',...) still prints in some cases:

Error: MySQL server has gone away
    at Error (native)

errors in my knex test code. I'll try to keep on reproducing that error and come back if I'm able to track the issue here :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sidorares picture sidorares  路  7Comments

abumusamq picture abumusamq  路  5Comments

HugoMuller picture HugoMuller  路  3Comments

SiroDiaz picture SiroDiaz  路  6Comments

magicxie picture magicxie  路  6Comments