fix: attach 'error' listeners on connections and channels - #56
Open
arsalan507 wants to merge 1 commit into
Open
Conversation
Node's EventEmitter throws (crashing the process) when an 'error' event has no listener. The producer had no listener on its connection or channel at all; the consumer had one on the connection but not on its channel. A broker bounce or channel-level protocol error could therefore crash the whole host process instead of being logged and handled. Attach an 'error' listener (logs via safeErrorMessage) on both the connection and the channel in Producer.connect() and Consumer.connect(). Extended the shared amqp-mock test helper (FakeChannel) with on()/ emitError() so tests can register and trigger channel-level 'error' listeners, mirroring the existing FakeConnection support. Fixes bitrix24#28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #28.
Node's
EventEmitterthrows synchronously (crashing the process, since it's an uncaught exception) when an'error'event is emitted with no registered listener. Before this change:RabbitMQProducer.connect()attached no'error'listener to either the connection or the channel.RabbitMQConsumer.connect()attached one to the connection, but not to the channel.So a broker bounce mid-publish, or a channel-level protocol error, could crash the whole host process instead of being logged and handled gracefully — same failure mode the existing connection-level consumer listener was already added to prevent.
Fix
Attach an
'error'listener (routed throughsafeErrorMessage, same as the existing connection-level listener) on:Producer.connect()Consumer.connect()(the connection listener already existed)Test plan
tests/_helpers/amqp-mock.tsFakeChannelwithon()/emitError(), mirroring the existingFakeConnectionsupport, so tests can register and trigger channel-level'error'listeners.tests/producer.test.ts(connection + channel) andtests/consumer.test.ts(channel) asserting a listener is registered and that emitting'error'logs via the configured logger without throwing. Verified all three fail against the pre-fix code (channel.on/connection.onnever called with'error') and pass after the fix.pnpm lint,pnpm typecheck,pnpm test(78 passing),pnpm buildall green.