node-ari-client icon indicating copy to clipboard operation
node-ari-client copied to clipboard

Error: Channel not found on hangup

Open uchepercynnoch opened this issue 1 year ago • 4 comments

Code Snippet

  private readonly url: string;
  private readonly username: string;
  private readonly password: string;

  constructor(config: ARIConfig) {
    this.url = config.url;
    this.username = config.username;
    this.password = config.password;
  }

  public async run() {
    const client = await this.getClient();

    client.once('StasisStart', (event, channel) => this.handleStasisStart(event, channel, client));
    client.once('StasisEnd', this.handleStasisEnd);

    await client.start('hello-world');
  }

  private async handleStasisStart(event: StasisStart, channel: Channel, client: Client) {
    const playback = client.Playback();

    await channel.ring();
    await appSetTimeout(2000);
    await channel.answer();
    await appSetTimeout(3000);
    await channel.play({ media: 'sound:hello-world' }, playback);
    await appSetTimeout(3000);
    await channel.hangup();
  }

  private async handleStasisEnd(event: StasisEnd, channel: Channel) {}

  private getClient(): Promise<Client> {
    return ari.connect(this.url, this.username, this.password);
  }

This works fine when I dial an extension. However, once I terminate the session, and try to hangup the session on StasisEnd handler, the above exception is thrown and node exists.

I can't seem to point out what is happening. This also happens when I use callbacks not just async/await.

Please help

Asterisk version - 18
Node version - 16
ARI client version - 2.2.0

uchepercynnoch avatar Apr 29 '23 13:04 uchepercynnoch

Same Here but The App runs fine if i didn't catch any error on Statsis end i likely think that because of the channel is destory while we are accessing the channel, That's why asterisk server return Channel Not Found.Hope Developer Fix it or we need to make a pull request

RizeKishimaro avatar Jan 13 '24 16:01 RizeKishimaro

Anyone have any ideas on this one? I'm having the same issue.

cculbreath avatar Jan 28 '24 01:01 cculbreath

It's always possible to try hanging up a channel that was already hung up, which will trigger an error. You should handle those and any other errors in your async functions using try-catch blocks.

public async run() {
	try {
		const client = await this.getClient();

		client.once('StasisStart', (event, channel) => this.handleStasisStart(event, channel, client));
		client.once('StasisEnd', this.handleStasisEnd);

		await client.start('hello-world');
	} catch (err) {
		console.log(err);
	}		
}
private async handleStasisStart(event: StasisStart, channel: Channel, client: Client) {
	try {
		const playback = client.Playback();

		await channel.ring();
		await appSetTimeout(2000);
		await channel.answer();
		await appSetTimeout(3000);
		await channel.play({ media: 'sound:hello-world' }, playback);
		await appSetTimeout(3000);
		await channel.hangup();

	} catch (err) {
		console.log(err);
	}
}

dioris-moreno avatar Feb 21 '24 04:02 dioris-moreno

@uchepercynnoch , you have screen or config of dialplan extension, the part of execute Stasis app

dorlanpabon avatar Mar 05 '24 13:03 dorlanpabon