Fix: throws in user event handlers are caught and re-emitted as handler-error#850
Merged
Conversation
…er-error (#334) Wraps all user-facing emit() calls on both Connection and Channel with safeEmit(). If a handler throws and a handler-error listener is registered, the error is delivered there via setImmediate (preventing re-entry into amqplib internals). If no handler-error listener is registered, behaviour is unchanged. Previously, throws in channel event handlers (ack, nack, cancel, error, close) would propagate through acceptLoop and kill the connection. Throws in connection close handlers were silently swallowed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…t in README handler-error listeners now receive (err, event) so the listener knows which event handler threw. Update README with dedicated handler-error section, error handler examples, and clearer log messages. Update CHANGELOG to reflect the new signature. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Apply safeEmit to the three remaining bare this.emit('error') calls in
Connection: closeWithError, onSocketError, and startHeartbeater. Adds
tests for all three paths in both the without and with handler-error
listener describe blocks.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Throws in user-supplied event handlers on
ConnectionandChannelare now caught and re-emitted as ahandler-errorevent (with the original error and event name as arguments), rather than propagating as uncaught exceptions or being silently swallowed.If no
handler-errorlistener is registered, the original behaviour is preserved — the error is rethrown.Closes #334.