Skip to content

fix: propagate channel errors to connection and handle channel close #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/amqp-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
constructor(connection: AMQPBaseClient, id: number) {
this.connection = connection
this.id = id
this.onerror = (reason: string) => this.logger?.error(`channel ${this.id} closed: ${reason}`)
this.onerror = (reason: string) => {
this.logger?.error(`channel ${this.id} closed: ${reason}`)
// Propagate channel errors to the connection's onerror handler
if (this.id !== 0) { // Don't propagate for connection channel (id=0)
this.connection.onerror(new AMQPError(reason, this.connection))
}
}
}

private get logger() {
Expand Down Expand Up @@ -762,7 +768,7 @@
* @param [err] - why the channel was closed
*/
setClosed(err?: Error): void {
const closedByServer = err !== undefined

Check failure on line 771 in src/amqp-channel.ts

View workflow job for this annotation

GitHub Actions / test-browser

'closedByServer' is declared but its value is never read.

Check failure on line 771 in src/amqp-channel.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

'closedByServer' is declared but its value is never read.

Check failure on line 771 in src/amqp-channel.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

'closedByServer' is declared but its value is never read.

Check failure on line 771 in src/amqp-channel.ts

View workflow job for this annotation

GitHub Actions / test (22.x)

'closedByServer' is declared but its value is never read.
err ||= new Error("Connection closed by client")
if (!this.closed) {
this.closed = true
Expand All @@ -772,7 +778,11 @@
// Reject and clear all unconfirmed publishes
this.unconfirmedPublishes.forEach(([, , reject]) => reject(err))
this.unconfirmedPublishes.length = 0
if (closedByServer) this.onerror(err.message)

// Call onerror for any error, whether from server or from a channel operation
if (err.message !== "Connection closed by client") {
this.onerror(err.message)
}
}
}

Expand Down
Loading