lib/client: add generic error handling to roundtrip()
This commit is contained in:
parent
8c8bd43638
commit
fc8aa30756
2 changed files with 50 additions and 40 deletions
|
@ -52,10 +52,11 @@ let lastWhoxToken = 0;
|
|||
class IRCError extends Error {
|
||||
constructor(msg) {
|
||||
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];
|
||||
} else {
|
||||
text = `unknown error (${msg.command} ${msg.params.join(" ")})`;
|
||||
text = `unknown error (${msg.command})`;
|
||||
}
|
||||
super(text);
|
||||
|
||||
|
@ -717,6 +718,8 @@ export default class Client extends EventTarget {
|
|||
/* Execute a command that expects a response. `done` is called with message
|
||||
* events until it returns a truthy value. */
|
||||
roundtrip(msg, done) {
|
||||
let cmd = msg.command;
|
||||
|
||||
let label;
|
||||
if (this.enabledCaps["labeled-response"]) {
|
||||
lastLabel++;
|
||||
|
@ -735,6 +738,24 @@ export default class Client extends EventTarget {
|
|||
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;
|
||||
try {
|
||||
result = done(msg);
|
||||
|
@ -784,23 +805,18 @@ export default class Client extends EventTarget {
|
|||
}
|
||||
}
|
||||
|
||||
switch (msg.command) {
|
||||
case "BATCH":
|
||||
let enter = msg.params[0].startsWith("+");
|
||||
let name = msg.params[0].slice(1);
|
||||
if (enter && msg.params[1] === batchType) {
|
||||
batchName = name;
|
||||
break;
|
||||
}
|
||||
if (!enter && name === batchName) {
|
||||
return { ...this.batches.get(name), messages };
|
||||
}
|
||||
break;
|
||||
case "FAIL":
|
||||
if (msg.params[0] === cmd) {
|
||||
throw new IRCError(msg);
|
||||
}
|
||||
break;
|
||||
if (msg.command !== "BATCH") {
|
||||
return;
|
||||
}
|
||||
|
||||
let enter = msg.params[0].startsWith("+");
|
||||
let name = msg.params[0].slice(1);
|
||||
if (enter && msg.params[1] === batchType) {
|
||||
batchName = name;
|
||||
return;
|
||||
}
|
||||
if (!enter && name === batchName) {
|
||||
return { ...this.batches.get(name), messages };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -950,20 +966,15 @@ export default class Client extends EventTarget {
|
|||
params: ["*", email || "*", password],
|
||||
};
|
||||
return this.roundtrip(msg, (msg) => {
|
||||
switch (msg.command) {
|
||||
case "REGISTER":
|
||||
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;
|
||||
if (msg.command !== "REGISTER") {
|
||||
return;
|
||||
}
|
||||
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],
|
||||
};
|
||||
return this.roundtrip(msg, (msg) => {
|
||||
switch (msg.command) {
|
||||
case "VERIFY":
|
||||
return { message: msg.params[2] };
|
||||
case "FAIL":
|
||||
if (msg.params[0] === "VERIFY") {
|
||||
throw new IRCError(msg);
|
||||
}
|
||||
break;
|
||||
if (msg.command !== "VERIFY") {
|
||||
return;
|
||||
}
|
||||
return { message: msg.params[2] };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue