Skip to content

Commit

Permalink
Channel#onerror callback
Browse files Browse the repository at this point in the history
If the channel is closed by the server this callback is called.

Fixes #40
  • Loading branch information
carlhoerberg committed Dec 14, 2022
1 parent 18873b5 commit b279e86
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/amqp-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ export class AMQPChannel {
delivery?: AMQPMessage
getMessage?: AMQPMessage
returned?: AMQPMessage
onerror: (reason: string) => void
/**
* @param connection - The connection this channel belongs to
* @param id - ID of the channel
*/
constructor(connection: AMQPBaseClient, id: number) {
this.connection = connection
this.id = id
this.onerror = (reason: string) => console.error(`channel ${this.id} closed: ${reason}`)
}

/**
Expand Down Expand Up @@ -762,6 +764,7 @@ export class AMQPChannel {
* @param [err] - why the channel was closed
*/
setClosed(err?: Error): void {
const closedByServer = err !== undefined
err ||= new Error("Connection closed by client")
if (!this.closed) {
this.closed = true
Expand All @@ -772,6 +775,7 @@ export class AMQPChannel {
// Reject and clear all unconfirmed publishes
this.unconfirmedPublishes.forEach(([, , reject]) => reject(err))
this.unconfirmedPublishes.length = 0
if (closedByServer) this.onerror(err.message)
}
}

Expand Down

0 comments on commit b279e86

Please sign in to comment.