Skip to content

Commit 1bb9fe6

Browse files
carlhoerbergclaude
authored andcommitted
fix: propagate channel errors to connection and handle channel close properly
- Modify onerror handler in AMQPChannel to propagate errors to the connection level - Ensure channel errors are properly handled regardless of closure reason - Prevent propagation from connection channel (id=0) to avoid recursion 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ea7bcca commit 1bb9fe6

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/amqp-channel.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ export class AMQPChannel {
2828
constructor(connection: AMQPBaseClient, id: number) {
2929
this.connection = connection
3030
this.id = id
31-
this.onerror = (reason: string) => this.logger?.error(`channel ${this.id} closed: ${reason}`)
31+
this.onerror = (reason: string) => {
32+
this.logger?.error(`channel ${this.id} closed: ${reason}`)
33+
// Propagate channel errors to the connection's onerror handler
34+
if (this.id !== 0) { // Don't propagate for connection channel (id=0)
35+
this.connection.onerror(new AMQPError(reason, this.connection))
36+
}
37+
}
3238
}
3339

3440
private get logger() {
@@ -760,7 +766,6 @@ export class AMQPChannel {
760766
* @param [err] - why the channel was closed
761767
*/
762768
setClosed(err?: Error): void {
763-
const closedByServer = err !== undefined
764769
err ||= new Error("Connection closed by client")
765770
if (!this.closed) {
766771
this.closed = true
@@ -770,7 +775,11 @@ export class AMQPChannel {
770775
// Reject and clear all unconfirmed publishes
771776
this.unconfirmedPublishes.forEach(([, , reject]) => reject(err))
772777
this.unconfirmedPublishes.length = 0
773-
if (closedByServer) this.onerror(err.message)
778+
779+
// Call onerror for any error, whether from server or from a channel operation
780+
if (err.message !== "Connection closed by client") {
781+
this.onerror(err.message)
782+
}
774783
}
775784
}
776785

0 commit comments

Comments
 (0)