lib/client: introduce IRCError

This commit is contained in:
Simon Ser 2021-12-04 17:05:34 +01:00
parent 30e3ec392f
commit 8c8bd43638

View file

@ -49,6 +49,20 @@ const WHOX_FIELDS = {
let lastLabel = 0;
let lastWhoxToken = 0;
class IRCError extends Error {
constructor(msg) {
let text;
if (irc.isError(msg.command) && msg) {
text = msg.params[msg.params.length - 1];
} else {
text = `unknown error (${msg.command} ${msg.params.join(" ")})`;
}
super(text);
this.msg = msg;
}
}
export default class Client extends EventTarget {
static Status = {
DISCONNECTED: "disconnected",
@ -412,7 +426,7 @@ export default class Client extends EventTarget {
case irc.ERR_SASLTOOLONG:
case irc.ERR_SASLABORTED:
case irc.ERR_SASLALREADY:
throw msg;
throw new IRCError(msg);
}
});
this.send(initialResp);
@ -528,7 +542,7 @@ export default class Client extends EventTarget {
case irc.ERR_NOSUCHNICK:
nick = msg.params[1];
if (this.cm(nick) === targetCM) {
throw msg;
throw new IRCError(msg);
}
break;
}
@ -784,7 +798,7 @@ export default class Client extends EventTarget {
break;
case "FAIL":
if (msg.params[0] === cmd) {
throw msg;
throw new IRCError(msg);
}
break;
}
@ -946,7 +960,7 @@ export default class Client extends EventTarget {
};
case "FAIL":
if (msg.params[0] === "REGISTER") {
throw msg;
throw new IRCError(msg);
}
break;
}
@ -964,7 +978,7 @@ export default class Client extends EventTarget {
return { message: msg.params[2] };
case "FAIL":
if (msg.params[0] === "VERIFY") {
throw msg;
throw new IRCError(msg);
}
break;
}