mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
...
This commit is contained in:
1 parent
2b71e9c642
commit
d9b529edd7
3 files changed
+278
-4
No files matched your search
@@ -8,6 +8,14 @@
|
|||||||
"retries": 3
|
"retries": 3
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"errorLogChannel": {
|
||||||
|
"enabled": false,
|
||||||
|
"channelId": "paste the ID of the channel that should receive error logs",
|
||||||
|
"forwardWarn": true,
|
||||||
|
"minIntervalMs": 1500,
|
||||||
|
"maxQueue": 40,
|
||||||
|
"Comment": "Forward error logs and crash reports to a Fluxer channel. Every logger.error line (and every logger.warn line when forwardWarn=true) is posted there: player failures, node disconnects, database errors, plus fatal crash reports (uncaughtException, unhandledRejection) with uptime and memory. Identical messages are deduplicated for 60s, sends are spaced by minIntervalMs, and floods beyond maxQueue pending messages are dropped with a summary line instead of spamming. channelId: enable Developer Mode in the Fluxer app, right-click the target channel and copy its ID — the bot must be able to send messages there. Fully disabled unless enabled=true AND channelId is set."
|
||||||
|
},
|
||||||
"prefix": "your desired prefix (such as %)",
|
"prefix": "your desired prefix (such as %)",
|
||||||
"presenceInterval": null,
|
"presenceInterval": null,
|
||||||
"presenceContents-Comment": "Each entry can be a plain string (e.g. 'Ping for Prefix') or an object. Objects support: text (required), emoji_name (unicode emoji or custom emoji name), emoji_id (snowflake ID for custom guild emojis only), activity ({ name, type, url? }). Activity types: 0=Playing, 1=Streaming, 2=Listening, 3=Watching, 5=Competing. For custom guild emojis, provide both emoji_name AND emoji_id. For unicode emojis, only emoji_name is needed.",
|
"presenceContents-Comment": "Each entry can be a plain string (e.g. 'Ping for Prefix') or an object. Objects support: text (required), emoji_name (unicode emoji or custom emoji name), emoji_id (snowflake ID for custom guild emojis only), activity ({ name, type, url? }). Activity types: 0=Playing, 1=Streaming, 2=Listening, 3=Watching, 5=Competing. For custom guild emojis, provide both emoji_name AND emoji_id. For unicode emojis, only emoji_name is needed.",
|
||||||
|
|||||||
@@ -7,9 +7,12 @@
|
|||||||
|
|
||||||
import { logger } from "./src/core/Logger.mjs";
|
import { logger } from "./src/core/Logger.mjs";
|
||||||
import Remix from "./src/core/Bot.mjs";
|
import Remix from "./src/core/Bot.mjs";
|
||||||
|
import { initErrorChannel, reportCrash } from "./src/core/ErrorChannel.mjs";
|
||||||
|
|
||||||
const remix = new Remix();
|
const remix = new Remix();
|
||||||
|
|
||||||
|
initErrorChannel({ config: remix.config, client: remix.client });
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether an error is a known-ignorable WebSocket transport crash from
|
* Check whether an error is a known-ignorable WebSocket transport crash from
|
||||||
* the fluxer.js ws or undici internals.
|
* the fluxer.js ws or undici internals.
|
||||||
@@ -83,10 +86,11 @@ const WS_CRASH_LOG_COOLDOWN = 30_000;
|
|||||||
process.on("unhandledRejection", (reason, p) => {
|
process.on("unhandledRejection", (reason, p) => {
|
||||||
if (reason?.message?.includes("AudioSource is closed")) return;
|
if (reason?.message?.includes("AudioSource is closed")) return;
|
||||||
logger.error("[Error_Handling] Unhandled Rejection/Catch");
|
logger.error("[Error_Handling] Unhandled Rejection/Catch");
|
||||||
logger.error("Reason:", reason, p);
|
logger.error("[Error_Handling] Reason:", reason, p);
|
||||||
|
reportCrash("Unhandled Rejection", reason, { fatal: false });
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on("uncaughtException", (err, origin) => {
|
process.on("uncaughtException", async (err, origin) => {
|
||||||
if (isIgnorableWsCrash(err)) {
|
if (isIgnorableWsCrash(err)) {
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (now - _lastWsCrashLog > WS_CRASH_LOG_COOLDOWN) {
|
if (now - _lastWsCrashLog > WS_CRASH_LOG_COOLDOWN) {
|
||||||
@@ -120,7 +124,8 @@ process.on("uncaughtException", (err, origin) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
logger.error("[Error_Handling] Uncaught Exception/Catch");
|
logger.error("[Error_Handling] Uncaught Exception/Catch");
|
||||||
logger.error("Error:", err, origin);
|
logger.error("[Error_Handling] Error:", err, origin);
|
||||||
|
await reportCrash("Uncaught Exception — bot is restarting", err, { fatal: true });
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -130,7 +135,8 @@ process.on("uncaughtExceptionMonitor", (err, origin) => {
|
|||||||
if (isBenignStreamAbortRace(err)) return;
|
if (isBenignStreamAbortRace(err)) return;
|
||||||
if (isBenignTransportError(err)) return;
|
if (isBenignTransportError(err)) return;
|
||||||
logger.error("[Error_Handling] Uncaught Exception/Catch (MONITOR)");
|
logger.error("[Error_Handling] Uncaught Exception/Catch (MONITOR)");
|
||||||
logger.error("Error:", err, origin);
|
logger.error("[Error_Handling] Error:", err, origin);
|
||||||
|
reportCrash("Uncaught Exception (MONITOR)", err, { fatal: false });
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,260 @@
|
|||||||
|
/**
|
||||||
|
* @module src/core/ErrorChannel
|
||||||
|
* @description Forward runtime errors, warnings and fatal crash reports to a
|
||||||
|
* designated Fluxer channel, so the owner sees problems (player failures,
|
||||||
|
* node disconnects, database errors, crashes) without reading container logs.
|
||||||
|
*
|
||||||
|
* Two forwarding layers:
|
||||||
|
* 1. Logger tap: wraps logger.error and (optionally) logger.warn so every
|
||||||
|
* existing error site in the codebase is covered without touching call
|
||||||
|
* sites. The logger's built-in storm gate already collapses identical
|
||||||
|
* repeated lines before they reach this tap.
|
||||||
|
* 2. Crash reports: explicit reportCrash() calls from the process-level
|
||||||
|
* handlers in index.mjs. These bypass the queue and send immediately —
|
||||||
|
* REST keeps working even when the gateway connection is down — with a
|
||||||
|
* bounded timeout so the fatal-exit path is never stalled.
|
||||||
|
*
|
||||||
|
* Messages are sent through client.rest.post when available, with a raw
|
||||||
|
* fetch fallback (Authorization: Bot <token>) for very early crashes before
|
||||||
|
* the REST client exists. A queue with a minimum send interval plus drop
|
||||||
|
* counting keeps the channel and API rate limits safe. Every failure inside
|
||||||
|
* this module is swallowed: error reporting must never cause a crash or
|
||||||
|
* alter runtime behavior.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import util from "node:util";
|
||||||
|
import { logger } from "./Logger.mjs";
|
||||||
|
|
||||||
|
const API_BASE = "https://api.fluxer.app/v1";
|
||||||
|
const SEND_TIMEOUT_MS = 5_000;
|
||||||
|
const CRASH_SEND_BUDGET_MS = 3_500;
|
||||||
|
const FORWARD_DEDUP_MS = 60_000;
|
||||||
|
const CRASH_DEDUP_MS = 30_000;
|
||||||
|
const FAIL_LOG_COOLDOWN_MS = 30_000;
|
||||||
|
const CONTENT_MAX = 1_900;
|
||||||
|
const BODY_MAX = 1_500;
|
||||||
|
|
||||||
|
let _enabled = false;
|
||||||
|
let _channelId = null;
|
||||||
|
let _token = null;
|
||||||
|
let _client = null;
|
||||||
|
let _minIntervalMs = 1_500;
|
||||||
|
let _maxQueue = 40;
|
||||||
|
|
||||||
|
let _queue = [];
|
||||||
|
let _dropped = 0;
|
||||||
|
let _flushing = false;
|
||||||
|
|
||||||
|
const _forwardDedup = new Map();
|
||||||
|
const _crashDedup = new Map();
|
||||||
|
let _lastFailLog = 0;
|
||||||
|
|
||||||
|
const _originals = { error: null, warn: null };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the channel reporter from config. Safe to call once at boot;
|
||||||
|
* subsequent calls only refresh options and never double-wrap the logger.
|
||||||
|
* @param {object} [opts]
|
||||||
|
* @param {object} [opts.config] - Full bot configuration (uses config.errorLogChannel and config.token).
|
||||||
|
* @param {object} [opts.client] - The Fluxer client (provides client.rest.post for sending).
|
||||||
|
*/
|
||||||
|
export function initErrorChannel({ config, client } = {}) {
|
||||||
|
const cfg = config?.errorLogChannel ?? {};
|
||||||
|
_channelId = typeof cfg.channelId === "string" && cfg.channelId.trim() ? cfg.channelId.trim() : null;
|
||||||
|
_enabled = cfg.enabled === true && Boolean(_channelId);
|
||||||
|
_token = typeof config?.token === "string" ? config.token : null;
|
||||||
|
_client = client ?? null;
|
||||||
|
_minIntervalMs = Number(cfg.minIntervalMs) > 0 ? Number(cfg.minIntervalMs) : 1_500;
|
||||||
|
_maxQueue = Number(cfg.maxQueue) > 0 ? Number(cfg.maxQueue) : 40;
|
||||||
|
|
||||||
|
if (!_enabled) {
|
||||||
|
console.info(`[ErrorChannel] Disabled (${!_channelId ? "no channelId configured" : "enabled=false"}).`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!_originals.error) _tapLogger(cfg.forwardWarn !== false);
|
||||||
|
console.info(`[ErrorChannel] Active — forwarding errors${cfg.forwardWarn !== false ? " + warnings" : ""} to channel ${_channelId}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Report a process-level event (crash, unhandled rejection, monitor catch).
|
||||||
|
* Bypasses the queue, deduplicates identical errors within a short window
|
||||||
|
* (prevents double-sends between the fatal handler and the MONITOR handler),
|
||||||
|
* and resolves within a bounded timeout so fatal paths can still exit.
|
||||||
|
* @async
|
||||||
|
* @param {string} title - Event headline shown in the channel.
|
||||||
|
* @param {*} err - The error (any value; formatted like console would).
|
||||||
|
* @param {object} [opts]
|
||||||
|
* @param {boolean} [opts.fatal=false] - True when the bot is about to exit.
|
||||||
|
* @returns {Promise<boolean>} True when the message was delivered.
|
||||||
|
*/
|
||||||
|
export async function reportCrash(title, err, { fatal = false } = {}) {
|
||||||
|
if (!_enabled || !_channelId) return false;
|
||||||
|
try {
|
||||||
|
const body = _formatArgs([err]).split("```").join("‹›");
|
||||||
|
const now = Date.now();
|
||||||
|
const key = body.slice(0, 300);
|
||||||
|
if (_crashDedup.get(key) && now - _crashDedup.get(key) < CRASH_DEDUP_MS) return false;
|
||||||
|
_crashDedup.set(key, now);
|
||||||
|
if (_crashDedup.size > 100) _crashDedup.delete(_crashDedup.keys().next().value);
|
||||||
|
|
||||||
|
const mem = Math.round(process.memoryUsage().rss / 1_048_576);
|
||||||
|
const content =
|
||||||
|
`${fatal ? "🚨" : "⚠️"} **${title}**\n${body.slice(0, BODY_MAX)}\n— uptime ${_uptime()} · rss ${mem}MB`
|
||||||
|
.slice(0, CONTENT_MAX);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await Promise.race([_send(content), _sleep(CRASH_SEND_BUDGET_MS)]);
|
||||||
|
return true;
|
||||||
|
} catch (_) {
|
||||||
|
await Promise.race([_sendRaw(content), _sleep(CRASH_SEND_BUDGET_MS)]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restore the untouched logger methods and clear all state (tests only).
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
|
export function _resetErrorChannelForTests() {
|
||||||
|
if (_originals.error) logger.error = _originals.error;
|
||||||
|
if (_originals.warn) logger.warn = _originals.warn;
|
||||||
|
_originals.error = null;
|
||||||
|
_originals.warn = null;
|
||||||
|
_enabled = false;
|
||||||
|
_channelId = null;
|
||||||
|
_token = null;
|
||||||
|
_client = null;
|
||||||
|
_queue = [];
|
||||||
|
_dropped = 0;
|
||||||
|
_flushing = false;
|
||||||
|
_forwardDedup.clear();
|
||||||
|
_crashDedup.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @private Wrap the shared logger methods so all existing error sites forward automatically. */
|
||||||
|
function _tapLogger(forwardWarn) {
|
||||||
|
_originals.error = logger.error;
|
||||||
|
logger.error = function (tag, ...args) {
|
||||||
|
_originals.error(tag, ...args);
|
||||||
|
_forward("error", tag, args);
|
||||||
|
};
|
||||||
|
if (forwardWarn) {
|
||||||
|
_originals.warn = logger.warn;
|
||||||
|
logger.warn = function (tag, ...args) {
|
||||||
|
_originals.warn(tag, ...args);
|
||||||
|
_forward("warn", tag, args);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @private Forward one emitted log line into the queue (with dedup + self-exclusion). */
|
||||||
|
function _forward(level, tag, args) {
|
||||||
|
try {
|
||||||
|
if (typeof tag !== "string") return;
|
||||||
|
if (tag.startsWith("[ErrorChannel]") || tag.startsWith("[Error_Handling]")) return;
|
||||||
|
const body = _formatArgs(args);
|
||||||
|
const now = Date.now();
|
||||||
|
const emoji = level === "error" ? "🔴" : "🟡";
|
||||||
|
const text = body ? `${emoji} **${tag}** ${body}` : `${emoji} **${tag}**`;
|
||||||
|
const key = `${level}|${tag}|${(body || tag).slice(0, 200)}`;
|
||||||
|
const last = _forwardDedup.get(key);
|
||||||
|
if (last && now - last < FORWARD_DEDUP_MS) return;
|
||||||
|
_forwardDedup.set(key, now);
|
||||||
|
if (_forwardDedup.size > 300) _forwardDedup.delete(_forwardDedup.keys().next().value);
|
||||||
|
_enqueue(text.slice(0, CONTENT_MAX));
|
||||||
|
} catch (_) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @private Format values the same way console would. */
|
||||||
|
function _formatArgs(args) {
|
||||||
|
try {
|
||||||
|
return util.format(...args).trim();
|
||||||
|
} catch (_) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @private Add a message to the bounded send queue and start the flusher. */
|
||||||
|
function _enqueue(content) {
|
||||||
|
if (_queue.length >= _maxQueue) {
|
||||||
|
_dropped++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_queue.push(content);
|
||||||
|
if (!_flushing) _runFlush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @private Drain the queue with rate-limit spacing, then report any dropped flood messages. */
|
||||||
|
async function _runFlush() {
|
||||||
|
_flushing = true;
|
||||||
|
try {
|
||||||
|
while (_queue.length > 0) {
|
||||||
|
const content = _queue.shift();
|
||||||
|
await _sendQuietly(content);
|
||||||
|
await _sleep(_minIntervalMs);
|
||||||
|
}
|
||||||
|
if (_dropped > 0) {
|
||||||
|
const n = _dropped;
|
||||||
|
_dropped = 0;
|
||||||
|
await _sendQuietly(`📦 ${n} additional error log message(s) were dropped during a flood to protect rate limits.`);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
_flushing = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @private Send a queued message, swallowing and cooldown-logging failures. */
|
||||||
|
async function _sendQuietly(content) {
|
||||||
|
try {
|
||||||
|
await _send(content);
|
||||||
|
} catch (e) {
|
||||||
|
const now = Date.now();
|
||||||
|
if (now - _lastFailLog > FAIL_LOG_COOLDOWN_MS) {
|
||||||
|
_lastFailLog = now;
|
||||||
|
console.error(`[ErrorChannel] Send failed: ${e?.message ?? e}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @private Send through the client's REST client when available, else raw fetch. */
|
||||||
|
async function _send(content) {
|
||||||
|
const rest = _client?.rest;
|
||||||
|
if (typeof rest?.post === "function") {
|
||||||
|
await rest.post(`/channels/${_channelId}/messages`, { content });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await _sendRaw(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @private Send via a direct Fluxer REST call (Authorization: Bot <token>). */
|
||||||
|
async function _sendRaw(content) {
|
||||||
|
const res = await fetch(`${API_BASE}/channels/${_channelId}/messages`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Authorization": `Bot ${_token}`, "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ content }),
|
||||||
|
signal: AbortSignal.timeout(SEND_TIMEOUT_MS),
|
||||||
|
});
|
||||||
|
if (!res.ok) {
|
||||||
|
const text = await res.text().catch(() => "");
|
||||||
|
throw new Error(`Fluxer API HTTP ${res.status}: ${text.slice(0, 200)}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @private Human-readable process uptime. */
|
||||||
|
function _uptime() {
|
||||||
|
const s = Math.floor(process.uptime());
|
||||||
|
const h = Math.floor(s / 3_600);
|
||||||
|
const m = Math.floor((s % 3_600) / 60);
|
||||||
|
return h > 0 ? `${h}h ${m}m` : `${m}m ${s % 60}s`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @private Sleep that never keeps the event loop alive by itself. */
|
||||||
|
function _sleep(ms) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const t = setTimeout(resolve, ms);
|
||||||
|
t.unref?.();
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in new issue
Block a user