mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 09:03:37 +01:00
The logger keeps errorCode only for allowlisted codes, and so does the collector (sanitize.vrl). Both lists are maintained by hand, so codes added since then were dropped and logged as a bare 400. Among them: invalid_billing_expiration (billingError), invalid_auth_state (appError), streamer_unreachable, github_unavailable, user_banned and most billing and GitHub App codes, 41 in all. scripts/generate-log-diagnostics.js now collects the codes passed to AnonymousError, billingError and appError, plus codes assigned to an errCode/code variable. It writes them to LOG_THROWN_ERROR_CODES, which the logger accepts, and appends them to the collector's errorCode allowlist. A test fails when a thrown code is missing from either list. Regenerating also refreshes source function names that had gone stale (jsDelivrFileUrl, ownerGitHubAccessRequired, ...), whose stack frames the collector was dropping. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
67 lines
3.2 KiB
JavaScript
67 lines
3.2 KiB
JavaScript
const { strict: assert } = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const ts = require("typescript");
|
|
require("ts-node/register/transpile-only");
|
|
const { logRecord, LOG_ERROR_CODES } = require("../src/core/log-record");
|
|
const { serializeError } = require("../src/core/logger");
|
|
const { billingError } = require("../src/core/billing/config");
|
|
const { consumeFlow } = require("../src/server/routes/github-app");
|
|
|
|
const root = path.join(__dirname, "..");
|
|
const vrl = fs.readFileSync(path.join(root, "deploy/logging/sanitize.vrl"), "utf8");
|
|
const collectorCodes = new Set(JSON.parse(vrl.match(/if includes\((\[.*?\]), record\.errorCode\)/)[1]));
|
|
|
|
// Independent of the generator: string codes passed straight to the error
|
|
// factories anywhere under src/.
|
|
function thrownCodes() {
|
|
const codes = new Map();
|
|
const literals = (node, file) => {
|
|
if (!node) return;
|
|
if (ts.isStringLiteralLike(node)) { if (/^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)+$/.test(node.text)) codes.set(node.text, file); }
|
|
else if (ts.isConditionalExpression(node)) { literals(node.whenTrue, file); literals(node.whenFalse, file); }
|
|
else if (ts.isParenthesizedExpression(node)) literals(node.expression, file);
|
|
};
|
|
const walk = (dir) => {
|
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
const file = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) { walk(file); continue; }
|
|
if (!file.endsWith(".ts") || file.endsWith(".d.ts")) continue;
|
|
const source = ts.createSourceFile(file, fs.readFileSync(file, "utf8"), ts.ScriptTarget.Latest, true);
|
|
(function visit(node) {
|
|
if ((ts.isNewExpression(node) || ts.isCallExpression(node)) && ts.isIdentifier(node.expression) &&
|
|
["AnonymousError", "billingError", "appError"].includes(node.expression.text) && node.arguments?.length) {
|
|
literals(node.arguments[0], path.relative(root, file));
|
|
}
|
|
ts.forEachChild(node, visit);
|
|
})(source);
|
|
}
|
|
};
|
|
walk(path.join(root, "src"));
|
|
return codes;
|
|
}
|
|
|
|
describe("thrown error codes reach the logs", () => {
|
|
it("keeps the billing expiration and GitHub App state codes", () => {
|
|
const billing = logRecord("warn", "route", [serializeError(billingError("invalid_billing_expiration"))]);
|
|
assert.equal(billing.errorCode, "invalid_billing_expiration");
|
|
let state;
|
|
try { consumeFlow(undefined, "state"); } catch (error) { state = error; }
|
|
assert.equal(state.message, "invalid_auth_state");
|
|
assert.equal(logRecord("warn", "route", [serializeError(state)]).errorCode, "invalid_auth_state");
|
|
for (const code of ["invalid_billing_expiration", "invalid_auth_state", "streamer_unreachable"]) {
|
|
assert.ok(collectorCodes.has(code), `${code} is missing from sanitize.vrl`);
|
|
}
|
|
});
|
|
|
|
it("allowlists every code passed to an error factory", () => {
|
|
const missing = [...thrownCodes()].filter(([code]) => !LOG_ERROR_CODES.has(code) || !collectorCodes.has(code))
|
|
.map(([code, file]) => `${code} (${file})`);
|
|
assert.deepEqual(missing, [], "run node scripts/generate-log-diagnostics.js");
|
|
});
|
|
|
|
it("lets the collector keep every code the logger emits", () => {
|
|
assert.deepEqual([...LOG_ERROR_CODES].filter((code) => !collectorCodes.has(code)), []);
|
|
});
|
|
});
|