Merge pull request #988 from tdurieux/fix/missing-error-codes

fix(logging): keep every thrown error code instead of a bare 400
This commit is contained in:
Thomas Durieux authored and GitHub committed 2026-11-29 03:44:20 -08:00
commit 2317544c5a
5 files changed
+110 -8

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
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)), []);
});
});