lib/client: add generic error handling to roundtrip()

This commit is contained in:
Simon Ser 2021-12-04 17:22:36 +01:00
parent 8c8bd43638
commit fc8aa30756
2 changed files with 50 additions and 40 deletions

View file

@ -52,10 +52,11 @@ let lastWhoxToken = 0;
class IRCError extends Error { class IRCError extends Error {
constructor(msg) { constructor(msg) {
let text; let text;
if (irc.isError(msg.command) && msg) { if (msg.params.length > 0) {
// IRC errors have a human-readable message as last param
text = msg.params[msg.params.length - 1]; text = msg.params[msg.params.length - 1];
} else { } else {
text = `unknown error (${msg.command} ${msg.params.join(" ")})`; text = `unknown error (${msg.command})`;
} }
super(text); super(text);
@ -717,6 +718,8 @@ export default class Client extends EventTarget {
/* Execute a command that expects a response. `done` is called with message /* Execute a command that expects a response. `done` is called with message
* events until it returns a truthy value. */ * events until it returns a truthy value. */
roundtrip(msg, done) { roundtrip(msg, done) {
let cmd = msg.command;
let label; let label;
if (this.enabledCaps["labeled-response"]) { if (this.enabledCaps["labeled-response"]) {
lastLabel++; lastLabel++;
@ -735,6 +738,24 @@ export default class Client extends EventTarget {
return; return;
} }
let isError = false;
switch (msg.command) {
case "FAIL":
isError = msg.params[0] === cmd;
break;
case irc.ERR_UNKNOWNERROR:
case irc.ERR_UNKNOWNCOMMAND:
case irc.ERR_NEEDMOREPARAMS:
case irc.RPL_TRYAGAIN:
isError = msg.params[1] === cmd;
break;
}
if (isError) {
removeEventListeners();
reject(new IRCError(msg));
return;
}
let result; let result;
try { try {
result = done(msg); result = done(msg);
@ -784,23 +805,18 @@ export default class Client extends EventTarget {
} }
} }
switch (msg.command) { if (msg.command !== "BATCH") {
case "BATCH": return;
let enter = msg.params[0].startsWith("+"); }
let name = msg.params[0].slice(1);
if (enter && msg.params[1] === batchType) { let enter = msg.params[0].startsWith("+");
batchName = name; let name = msg.params[0].slice(1);
break; if (enter && msg.params[1] === batchType) {
} batchName = name;
if (!enter && name === batchName) { return;
return { ...this.batches.get(name), messages }; }
} if (!enter && name === batchName) {
break; return { ...this.batches.get(name), messages };
case "FAIL":
if (msg.params[0] === cmd) {
throw new IRCError(msg);
}
break;
} }
}); });
} }
@ -950,20 +966,15 @@ export default class Client extends EventTarget {
params: ["*", email || "*", password], params: ["*", email || "*", password],
}; };
return this.roundtrip(msg, (msg) => { return this.roundtrip(msg, (msg) => {
switch (msg.command) { if (msg.command !== "REGISTER") {
case "REGISTER": return;
let result = msg.params[0];
return {
verificationRequired: result === "VERIFICATION_REQUIRED",
account: msg.params[1],
message: msg.params[2],
};
case "FAIL":
if (msg.params[0] === "REGISTER") {
throw new IRCError(msg);
}
break;
} }
let result = msg.params[0];
return {
verificationRequired: result === "VERIFICATION_REQUIRED",
account: msg.params[1],
message: msg.params[2],
};
}); });
} }
@ -973,15 +984,10 @@ export default class Client extends EventTarget {
params: [account, code], params: [account, code],
}; };
return this.roundtrip(msg, (msg) => { return this.roundtrip(msg, (msg) => {
switch (msg.command) { if (msg.command !== "VERIFY") {
case "VERIFY": return;
return { message: msg.params[2] };
case "FAIL":
if (msg.params[0] === "VERIFY") {
throw new IRCError(msg);
}
break;
} }
return { message: msg.params[2] };
}); });
} }
} }

View file

@ -5,6 +5,7 @@ export const RPL_CREATED = "003";
export const RPL_MYINFO = "004"; export const RPL_MYINFO = "004";
export const RPL_ISUPPORT = "005"; export const RPL_ISUPPORT = "005";
export const RPL_UMODEIS = "221"; export const RPL_UMODEIS = "221";
export const RPL_TRYAGAIN = "263";
export const RPL_AWAY = "301"; export const RPL_AWAY = "301";
export const RPL_WHOISUSER = "311"; export const RPL_WHOISUSER = "311";
export const RPL_WHOISSERVER = "312"; export const RPL_WHOISSERVER = "312";
@ -31,11 +32,14 @@ export const RPL_ENDOFBANLIST = "368";
export const RPL_MOTD = "372"; export const RPL_MOTD = "372";
export const RPL_MOTDSTART = "375"; export const RPL_MOTDSTART = "375";
export const RPL_ENDOFMOTD = "376"; export const RPL_ENDOFMOTD = "376";
export const ERR_UNKNOWNERROR = "400";
export const ERR_NOSUCHNICK = "401"; export const ERR_NOSUCHNICK = "401";
export const ERR_UNKNOWNCOMMAND = "421";
export const ERR_NOMOTD = "422"; export const ERR_NOMOTD = "422";
export const ERR_ERRONEUSNICKNAME = "432"; export const ERR_ERRONEUSNICKNAME = "432";
export const ERR_NICKNAMEINUSE = "433"; export const ERR_NICKNAMEINUSE = "433";
export const ERR_NICKCOLLISION = "436"; export const ERR_NICKCOLLISION = "436";
export const ERR_NEEDMOREPARAMS = "461";
export const ERR_NOPERMFORHOST = "463"; export const ERR_NOPERMFORHOST = "463";
export const ERR_PASSWDMISMATCH = "464"; export const ERR_PASSWDMISMATCH = "464";
export const ERR_YOUREBANNEDCREEP = "465"; export const ERR_YOUREBANNEDCREEP = "465";