I am having a node.js
application that uses amqlib
to connect with RabbitMQ.
I am trying to reproduce a connectivity error with RabbitMQ and I get two different errors by repeating the same flow.
What I am doing is:
- Start a Docker container with RabbitMQ management.
- Start a node.js application (either docker or with npm) that connects on the RabbitMQ.
- Go on RabbitMQ management and with
rabbitmqctl
execute thestop_app
This flow produces, each time one of the below two exceptions (not sure how it decides each one):
- OperationalError: connect ECONNREFUSED 172.24.0.3:5672
- Error: Heartbeat timeout
Why does this happen? Also, what is the best approach to handle them?
This is my connect function on the connector that does not seem to cover the heartbeat exception:
async connect(): Promise<Connection> {
const conn = await amqp.connect({
protocol: AMQP_PROTOCOL,
hostname: RABBITMQ_HOST,
port: Number(RABBITMQ_PORT),
username: RABBITMQ_USER,
password: RABBITMQ_PASS,
vhost: RABBITMQ_VHOST
});
conn.on('error', this.onError);
conn.on('close', this.onClose);
logger.debug('Connected to amqp');
this.conn = conn;
this.emit('connect', conn);
return conn;
}