fix(ui/core): add transient network retries for MessageHandler and suppress upstream socket drop errors

- Add isTransientNetworkError detection in MessageHandler for UND_ERR_SOCKET, fetch failed, and 502-504 responses
- Wrap reply, replyEmbed, sendMessage, sendEmbed, and editEmbed in exponential-backoff retry loops with channel fallback
- Update CommandLoader to log transient network drops as warnings instead of forwarding false-positive system error telemetry
This commit is contained in:
NoLogicAlan committed 2026-11-11 00:33:37 +03:00
1 parent 36139837ba
commit 4bade0869d
2 files changed
+201 -78

No files matched your search

+13
View File
@@ -49,6 +49,15 @@ export class CommandLoader {
err?.code === "COMMUNICATION_DISABLED" || err?.code === "COMMUNICATION_DISABLED" ||
err?.statusCode === 403; err?.statusCode === 403;
}; };
const isNetworkError = (err) => {
if (!err) return false;
const status = Number(err.statusCode ?? err.status);
if (status === 502 || status === 503 || status === 504 || status === 520 || status === 521 || status === 522 || status === 524) return true;
const code = err.code || err.cause?.code || err.cause?.cause?.code;
if (code === "UND_ERR_SOCKET" || code === "ECONNRESET" || code === "ETIMEDOUT") return true;
const str = `${err.message || ""} ${err.cause?.message || ""}`.toLowerCase();
return str.includes("other side closed") || str.includes("fetch failed");
};
const sendErrorReply = (id) => { const sendErrorReply = (id) => {
Promise.resolve(data.message.reply("An error occurred. Error id: `#" + id + "`")) Promise.resolve(data.message.reply("An error occurred. Error id: `#" + id + "`"))
.catch(err => logger.warn("[CommandHandler] Failed to send error-reply; error id #" + id + ":", err?.message)); .catch(err => logger.warn("[CommandHandler] Failed to send error-reply; error id #" + id + ":", err?.message));
@@ -58,6 +67,10 @@ export class CommandLoader {
logger.warn(`[CommandHandler] Missing permissions in channel ${data.message?.channelId ?? data.message?.channel?.id}: ${e.code ?? e.message}`); logger.warn(`[CommandHandler] Missing permissions in channel ${data.message?.channelId ?? data.message?.channel?.id}: ${e.code ?? e.message}`);
return; return;
} }
if (isNetworkError(e)) {
logger.warn(`[CommandHandler] Transient network or socket closure during command '${data.command?.name ?? "unknown"}': ${e.code ?? e.message}`);
return;
}
const id = Utils.uid(); const id = Utils.uid();
logger.error("Error running command; error id #" + id, e); logger.error("Error running command; error id #" + id, e);
sendErrorReply(id); sendErrorReply(id);
+188 -78
View File
@@ -380,6 +380,46 @@ export class MessageHandler {
}; };
} }
/**
* Determine if an error is a transient socket, connection, or gateway HTTP failure.
* @param {Error|object} err
* @returns {boolean}
*/
isTransientNetworkError(err) {
if (!err) return false;
const status = Number(err.statusCode ?? err.status);
if (status === 502 || status === 503 || status === 504 || status === 520 || status === 521 || status === 522 || status === 524) {
return true;
}
const codes = new Set([
"UND_ERR_SOCKET",
"UND_ERR_CONNECT_TIMEOUT",
"UND_ERR_HEADERS_TIMEOUT",
"UND_ERR_BODY_TIMEOUT",
"ECONNRESET",
"ETIMEDOUT",
"EPIPE",
"EAI_AGAIN",
"ENOTFOUND",
"ECONNREFUSED",
]);
if (
codes.has(err.code) ||
codes.has(err.cause?.code) ||
codes.has(err.cause?.cause?.code)
) {
return true;
}
const full = `${err.message || ""} ${err.cause?.message || ""} ${err.cause?.cause?.message || ""}`.toLowerCase();
return (
full.includes("other side closed") ||
full.includes("fetch failed") ||
full.includes("socket hung up") ||
full.includes("network socket disconnected") ||
full.includes("econnreset")
);
}
/** /**
* @async Reply to a message with permission checks. * @async Reply to a message with permission checks.
* @param {object} replyingTo * @param {object} replyingTo
@@ -395,28 +435,58 @@ export class MessageHandler {
} else { } else {
opts = { ...message }; opts = { ...message };
} }
try {
return new Message(await replyingTo.reply(opts, { ping: false }), this); const MAX_ATTEMPTS = 3;
} catch (err) { const RETRY_DELAY_MS = 250;
if (err.code === ErrorCodes.MessageNotFound || err.code === "UNKNOWN_MESSAGE" || err.statusCode === 404 || String(err?.message ?? "").includes("Message wasn't found")) {
/* Referenced message was deleted before reply arrived — fall back to channel send */ for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
try { try {
const ch = replyingTo.channel ?? (typeof this.getChannel === "function" ? this.getChannel(replyingTo.channelId) : null); return new Message(await replyingTo.reply(opts, { ping: false }), this);
if (ch && typeof ch.send === "function") { } catch (err) {
return new Message(await ch.send(opts), this); if (err.code === ErrorCodes.MessageNotFound || err.code === "UNKNOWN_MESSAGE" || err.statusCode === 404 || String(err?.message ?? "").includes("Message wasn't found")) {
/* Referenced message was deleted before reply arrived — fall back to channel send */
try {
const ch = replyingTo.channel ?? (typeof this.getChannel === "function" ? this.getChannel(replyingTo.channelId) : null);
if (ch && typeof ch.send === "function") {
return new Message(await ch.send(opts), this);
}
} catch (fallbackErr) {
logger.warn(`[MessageHandler] Fallback channel send failed in ${replyingTo.channelId ?? replyingTo.channel?.id}: ${fallbackErr.code ?? fallbackErr.message}`);
return null;
} }
} catch (fallbackErr) {
logger.warn(`[MessageHandler] Fallback channel send failed in ${replyingTo.channelId ?? replyingTo.channel?.id}: ${fallbackErr.code ?? fallbackErr.message}`);
return null; return null;
} }
return null;
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
logger.warn(`[MessageHandler] Cannot reply in channel ${replyingTo.channelId ?? replyingTo.channel?.id}: ${err.code ?? err.message}`);
return null;
}
if (this.isTransientNetworkError(err) && attempt < MAX_ATTEMPTS) {
logger.warn(`[MessageHandler] reply: transient network error (${err.code ?? err.message}) on attempt ${attempt}/${MAX_ATTEMPTS}, retrying in ${RETRY_DELAY_MS * attempt}ms…`);
await new Promise((r) => setTimeout(r, RETRY_DELAY_MS * attempt));
continue;
}
/* If socket closed persistently on reply, try fallback channel send before failing */
if (this.isTransientNetworkError(err)) {
try {
const ch = replyingTo.channel ?? (typeof this.getChannel === "function" ? this.getChannel(replyingTo.channelId) : null);
if (ch && typeof ch.send === "function") {
logger.warn(`[MessageHandler] reply: socket closed on reply, attempting fallback channel send in ${replyingTo.channelId ?? replyingTo.channel?.id}`);
return new Message(await ch.send(opts), this);
}
} catch (fallbackErr) {
logger.warn(`[MessageHandler] Fallback channel send failed: ${fallbackErr.code ?? fallbackErr.message}`);
}
logger.warn(`[MessageHandler] reply: network socket unavailable after ${MAX_ATTEMPTS} attempts: ${err.code ?? err.message}`);
return null;
}
throw err;
} }
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
logger.warn(`[MessageHandler] Cannot reply in channel ${replyingTo.channelId ?? replyingTo.channel?.id}: ${err.code ?? err.message}`);
return null;
}
throw err;
} }
return null;
} }
/** /**
@@ -429,16 +499,36 @@ export class MessageHandler {
async replyEmbed(replyingTo, message, options = {}) { async replyEmbed(replyingTo, message, options = {}) {
if (!(await this.assertPermissions(["SendMessages", "EmbedLinks"], replyingTo))) return null; if (!(await this.assertPermissions(["SendMessages", "EmbedLinks"], replyingTo))) return null;
let payload;
if (typeof message === "object" && Array.isArray(message.embeds)) { if (typeof message === "object" && Array.isArray(message.embeds)) {
payload = message;
} else {
options = {
mention: false,
embed: {},
...options
};
const content = (typeof message === "object") ? message.embedText : message;
payload = this.#createEmbed(content, options.embed);
if (typeof message === "object") {
const { embedText, ...rest } = message;
payload = { ...payload, ...rest };
}
}
const MAX_ATTEMPTS = 3;
const RETRY_DELAY_MS = 250;
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
try { try {
return new Message(await replyingTo.reply(message, { ping: false }), this); return new Message(await replyingTo.reply(payload, { ping: false }), this);
} catch (err) { } catch (err) {
if (err.code === ErrorCodes.MessageNotFound || err.code === "UNKNOWN_MESSAGE" || err.statusCode === 404 || String(err?.message ?? "").includes("Message wasn't found")) { if (err.code === ErrorCodes.MessageNotFound || err.code === "UNKNOWN_MESSAGE" || err.statusCode === 404 || String(err?.message ?? "").includes("Message wasn't found")) {
/* Referenced message was deleted before reply arrived — fall back to channel send */ /* Referenced message was deleted before reply arrived — fall back to channel send */
try { try {
const ch = replyingTo.channel ?? (typeof this.getChannel === "function" ? this.getChannel(replyingTo.channelId) : null); const ch = replyingTo.channel ?? (typeof this.getChannel === "function" ? this.getChannel(replyingTo.channelId) : null);
if (ch && typeof ch.send === "function") { if (ch && typeof ch.send === "function") {
return new Message(await ch.send(message), this); return new Message(await ch.send(payload), this);
} }
} catch (fallbackErr) { } catch (fallbackErr) {
logger.warn(`[MessageHandler] Fallback channel send failed in ${replyingTo.channelId ?? replyingTo.channel?.id}: ${fallbackErr.code ?? fallbackErr.message}`); logger.warn(`[MessageHandler] Fallback channel send failed in ${replyingTo.channelId ?? replyingTo.channel?.id}: ${fallbackErr.code ?? fallbackErr.message}`);
@@ -446,47 +536,36 @@ export class MessageHandler {
} }
return null; return null;
} }
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) { if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
logger.warn(`[MessageHandler] Cannot replyEmbed in channel ${replyingTo.channelId ?? replyingTo.channel?.id}: ${err.code ?? err.message}`); logger.warn(`[MessageHandler] Cannot replyEmbed in channel ${replyingTo.channelId ?? replyingTo.channel?.id}: ${err.code ?? err.message}`);
return null; return null;
} }
if (this.isTransientNetworkError(err) && attempt < MAX_ATTEMPTS) {
logger.warn(`[MessageHandler] replyEmbed: transient network error (${err.code ?? err.message}) on attempt ${attempt}/${MAX_ATTEMPTS}, retrying in ${RETRY_DELAY_MS * attempt}ms…`);
await new Promise((r) => setTimeout(r, RETRY_DELAY_MS * attempt));
continue;
}
if (this.isTransientNetworkError(err)) {
try {
const ch = replyingTo.channel ?? (typeof this.getChannel === "function" ? this.getChannel(replyingTo.channelId) : null);
if (ch && typeof ch.send === "function") {
logger.warn(`[MessageHandler] replyEmbed: socket closed on reply, attempting fallback channel send in ${replyingTo.channelId ?? replyingTo.channel?.id}`);
return new Message(await ch.send(payload), this);
}
} catch (fallbackErr) {
logger.warn(`[MessageHandler] Fallback channel send failed: ${fallbackErr.code ?? fallbackErr.message}`);
}
logger.warn(`[MessageHandler] replyEmbed: network socket unavailable after ${MAX_ATTEMPTS} attempts: ${err.code ?? err.message}`);
return null;
}
throw err; throw err;
} }
} }
return null;
options = {
mention: false,
embed: {},
...options
};
const content = (typeof message === "object") ? message.embedText : message;
let payload = this.#createEmbed(content, options.embed);
if (typeof message === "object") {
const { embedText, ...rest } = message;
payload = { ...payload, ...rest };
}
try {
return new Message(await replyingTo.reply(payload, { ping: false }), this);
} catch (err) {
if (err.code === ErrorCodes.MessageNotFound || err.code === "UNKNOWN_MESSAGE" || err.statusCode === 404 || String(err?.message ?? "").includes("Message wasn't found")) {
/* Referenced message was deleted before reply arrived — fall back to channel send */
try {
const ch = replyingTo.channel ?? (typeof this.getChannel === "function" ? this.getChannel(replyingTo.channelId) : null);
if (ch && typeof ch.send === "function") {
return new Message(await ch.send(payload), this);
}
} catch (fallbackErr) {
logger.warn(`[MessageHandler] Fallback channel send failed in ${replyingTo.channelId ?? replyingTo.channel?.id}: ${fallbackErr.code ?? fallbackErr.message}`);
return null;
}
return null;
}
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
logger.warn(`[MessageHandler] Cannot replyEmbed in channel ${replyingTo.channelId ?? replyingTo.channel?.id}: ${err.code ?? err.message}`);
return null;
}
throw err;
}
} }
/** /**
@@ -506,15 +585,31 @@ export class MessageHandler {
} else { } else {
opts = message; opts = message;
} }
try {
return new Message(await channel.send(opts), this); const MAX_ATTEMPTS = 3;
} catch (err) { const RETRY_DELAY_MS = 250;
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
logger.warn(`[MessageHandler] Cannot sendMessage in channel ${channel.id}: ${err.code ?? err.message}`); for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
return null; try {
return new Message(await channel.send(opts), this);
} catch (err) {
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
logger.warn(`[MessageHandler] Cannot sendMessage in channel ${channel.id}: ${err.code ?? err.message}`);
return null;
}
if (this.isTransientNetworkError(err) && attempt < MAX_ATTEMPTS) {
logger.warn(`[MessageHandler] sendMessage: transient network error (${err.code ?? err.message}) on attempt ${attempt}/${MAX_ATTEMPTS}, retrying in ${RETRY_DELAY_MS * attempt}ms…`);
await new Promise((r) => setTimeout(r, RETRY_DELAY_MS * attempt));
continue;
}
if (this.isTransientNetworkError(err)) {
logger.warn(`[MessageHandler] sendMessage: network socket unavailable in channel ${channel.id} after ${MAX_ATTEMPTS} attempts: ${err.code ?? err.message}`);
return null;
}
throw err;
} }
throw err;
} }
return null;
} }
/** /**
@@ -528,32 +623,42 @@ export class MessageHandler {
if (this.checkPermissions(["SendMessages", "EmbedLinks"], channel).length !== 0) { if (this.checkPermissions(["SendMessages", "EmbedLinks"], channel).length !== 0) {
return this.sendMessage(channel, typeof content === "string" ? content : content?.embedText ?? ""); return this.sendMessage(channel, typeof content === "string" ? content : content?.embedText ?? "");
} }
let payload;
if (typeof content === "object" && Array.isArray(content.embeds)) { if (typeof content === "object" && Array.isArray(content.embeds)) {
payload = content;
} else {
const text = (typeof content === "object") ? content.embedText : content;
payload = this.#createEmbed(text, embedOptions);
if (typeof content === "object") {
const { embedText, ...rest } = content;
Object.assign(payload, rest);
}
}
const MAX_ATTEMPTS = 3;
const RETRY_DELAY_MS = 250;
for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
try { try {
return new Message(await channel.send(content), this); return new Message(await channel.send(payload), this);
} catch (err) { } catch (err) {
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) { if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
logger.warn(`[MessageHandler] Cannot sendEmbed in channel ${channel.id}: ${err.code ?? err.message}`); logger.warn(`[MessageHandler] Cannot sendEmbed in channel ${channel.id}: ${err.code ?? err.message}`);
return null; return null;
} }
if (this.isTransientNetworkError(err) && attempt < MAX_ATTEMPTS) {
logger.warn(`[MessageHandler] sendEmbed: transient network error (${err.code ?? err.message}) on attempt ${attempt}/${MAX_ATTEMPTS}, retrying in ${RETRY_DELAY_MS * attempt}ms…`);
await new Promise((r) => setTimeout(r, RETRY_DELAY_MS * attempt));
continue;
}
if (this.isTransientNetworkError(err)) {
logger.warn(`[MessageHandler] sendEmbed: network socket unavailable in channel ${channel.id} after ${MAX_ATTEMPTS} attempts: ${err.code ?? err.message}`);
return null;
}
throw err; throw err;
} }
} }
const text = (typeof content === "object") ? content.embedText : content; return null;
const payload = this.#createEmbed(text, embedOptions);
if (typeof content === "object") {
const { embedText, ...rest } = content;
Object.assign(payload, rest);
}
try {
return new Message(await channel.send(payload), this);
} catch (err) {
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
logger.warn(`[MessageHandler] Cannot sendEmbed in channel ${channel.id}: ${err.code ?? err.message}`);
return null;
}
throw err;
}
} }
/** /**
@@ -595,12 +700,17 @@ export class MessageHandler {
return null; return null;
} }
if (RETRYABLE.has(err.statusCode) && attempt < MAX_ATTEMPTS) { if ((RETRYABLE.has(err.statusCode) || this.isTransientNetworkError(err)) && attempt < MAX_ATTEMPTS) {
logger.warn(`[MessageHandler] editEmbed: ${err.statusCode} on attempt ${attempt}/${MAX_ATTEMPTS}, retrying in ${RETRY_DELAY_MS}ms…`); logger.warn(`[MessageHandler] editEmbed: ${err.statusCode ?? err.code ?? err.message} on attempt ${attempt}/${MAX_ATTEMPTS}, retrying in ${RETRY_DELAY_MS}ms…`);
await new Promise(r => setTimeout(r, RETRY_DELAY_MS)); await new Promise(r => setTimeout(r, RETRY_DELAY_MS));
continue; continue;
} }
if (this.isTransientNetworkError(err)) {
logger.warn(`[MessageHandler] editEmbed: network socket unavailable after ${MAX_ATTEMPTS} attempts: ${err.code ?? err.message}`);
return null;
}
throw err; throw err;
} }
} }