jeromeludmann / deno-irc

IRC client protocol module for Deno
https://deno.land/x/irc
MIT License
12 stars 4 forks source link

client.quit() raises "error: Uncaught (in promise) Interrupted: operation canceled" #3

Closed kyanny closed 2 years ago

kyanny commented 2 years ago

Hi, when I ran the following code, it exits with the error. The code is almost same to https://github.com/jeromeludmann/deno-irc#usage and I added client.quit();. How can I properly quit from the IRC channel and disconnect from the IRC server?

import { Client } from "https://deno.land/x/irc/mod.ts";

const client = new Client({
  nick: "bot",
  channels: ["#general"],
});

client.on("join", (msg) => {
  if (msg.channel === "#general") {
    client.privmsg("#general", "Hello world!");
  }
  client.quit();
});

// no TLS
await client.connect("127.0.0.1", 6667);
❯ deno run -A irc-bot.ts
Check file:///Users/kyanny/workspaces/deno/irc-bot/irc-bot.ts
error: Uncaught (in promise) Interrupted: operation canceled
      read = await conn.read(this.buffer);
             ^
    at deno:core/01_core.js:106:46
    at unwrapOpResult (deno:core/01_core.js:126:13)
    at async read (deno:ext/net/01_net.js:21:19)
    at async Client.read (https://deno.land/x/irc@v0.5.0/core/client.ts:149:14)
    at async Client.loop (https://deno.land/x/irc@v0.5.0/core/client.ts:132:22)
jeromeludmann commented 2 years ago

Currently, there are two ways to avoid this error.

The easiest way is to use client.disconnect() instead of client.quit() (but you lose the ability to pass quit message):

client.on("join", (msg) => {
  if (msg.channel === "#general") {
    client.privmsg("#general", "Hello world!");
  }
  client.disconnect(); // <-- instead of client.quit()
});

If you still want to use client.quit(), you can add an error listener like that:

client.on("error", (err) => {
  if (err.type === "read" && err.name === "Interrupted") return;
  throw err;
});

This behavior needs to be fixed, thank you for noticing it 👍

kyanny commented 2 years ago

Thank you for providing workarounds, @jeromeludmann! Both works perfect to me. 👍