Skip to content

connection event connectFailed is emitted if connect event handler throws an error #374

Description

@krzysztofmeler-rasp

Hey, I found weird behavior of listeners for IAmqpConnectionManager object: if my connect event handler throws an error, for instance: it can't read parameters of undefined, connectFailed event is emitted. I don't think this should be the case. Sample of code:

Logger.info("Initialising amqp connection for task queue channel")
const amqpConnection = amqp.connect({ url: "amqp://urlRemovedByMeForGithubIssue" }, { heartbeatIntervalInSeconds: 10 });

amqpConnection.on('disconnect', (err) => {
  Logger.error(`amqp disconnected, ${serializeError(err)}`);
  this.#queueConnectionOk = false;
  this.#stopStatsGathering();
})

amqpConnection.on('connectFailed', ({err, url}) => {
  Logger.error(`amqp connection attempt failed, ${url}, ${serializeError(err)}`);
})

amqpConnection.on('blocked', ({reason}) => {
  Logger.warn(`amqp connection blocked by broker, reason: ${reason}`);
})

amqpConnection.on('unblocked', () => {
  Logger.info(`amqp connection unblocked by broker`);
})

amqpConnection.on('connect', ({connection, options}) => {
  Logger.info(`amqp connection established for uri ${options.url}`);
  this.#openChannel(connection);
})

} catch (e) {
Logger.error(`Error during creation of new amqp connection for channel ${config.rabbit.queueName}, ${serializeError(e)}`);
throw e;
}

Logger is our custom class of logger, don't be bothered by it.
It produces such log:

<start of log removed by me, time, server name etc.>: amqp connection attempt failed, amqp://urlRemovedByMeForGithubIssue, {"stack":"TypeError: Cannot read properties of undefined (reading 'url')
at AmqpConnectionManager. (/app/src/amqp-conn.js:92:68)
at AmqpConnectionManager.emit (node:events:524:28)
at /app/node_modules/amqp-connection-manager/dist/cjs/AmqpConnectionManager.js:289:22
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
"message":"Cannot read properties of undefined (reading 'url')"}

I know what causes this error of reading undefined but don't know why it's seems to be caught by connectFailed event emitter instead of being thrown and break my running node program.

Can you confirm that this behavior is wanted and not an issue or if it's an issue and you would like to fix it or get help with it?

I often write ampq instead of amqp, sorry for mistakes if such are present in description

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions