mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-12-02 17:13:33 +01:00
fix: keep every thrown error code in the logs
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>
This commit is contained in:
1 parent
a4f65e7d30
commit
63343c871c
5 files changed
+110
-8
No files matched your search
@@ -1,6 +1,18 @@
|
||||
// Generate finite diagnostic allowlists from repository code, never runtime input.
|
||||
const fs = require('fs'), path = require('path'), ts = require('typescript');
|
||||
const files = {};
|
||||
// Error codes the code base throws or relays, so the logger and the collector
|
||||
// keep them as errorCode instead of dropping them as arbitrary strings.
|
||||
const codePattern = /^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)+$/;
|
||||
const errorFactories = new Set(['AnonymousError','billingError','appError']);
|
||||
const thrownCodes = new Set();
|
||||
function codeLiterals(node) {
|
||||
if (!node) return;
|
||||
if (ts.isStringLiteralLike(node)) { if (codePattern.test(node.text)) thrownCodes.add(node.text); }
|
||||
else if (ts.isConditionalExpression(node)) { codeLiterals(node.whenTrue); codeLiterals(node.whenFalse); }
|
||||
else if (ts.isBinaryExpression(node) && [ts.SyntaxKind.BarBarToken, ts.SyntaxKind.QuestionQuestionToken].includes(node.operatorToken.kind)) { codeLiterals(node.left); codeLiterals(node.right); }
|
||||
else if (ts.isParenthesizedExpression(node)) codeLiterals(node.expression);
|
||||
}
|
||||
function scan(dir) {
|
||||
for (const entry of fs.readdirSync(dir, {withFileTypes:true})) {
|
||||
const file = path.join(dir,entry.name);
|
||||
@@ -11,6 +23,11 @@ function scan(dir) {
|
||||
function visit(node) {
|
||||
if ((ts.isFunctionDeclaration(node)||ts.isMethodDeclaration(node)||ts.isFunctionExpression(node)) && node.name && ts.isIdentifier(node.name)) names.add(node.name.text);
|
||||
if(ts.isVariableDeclaration(node)&&ts.isIdentifier(node.name)&&node.initializer&&(ts.isArrowFunction(node.initializer)||ts.isFunctionExpression(node.initializer))) names.add(node.name.text);
|
||||
// new AnonymousError("code"), billingError("code"), appError("code")
|
||||
if((ts.isNewExpression(node)||ts.isCallExpression(node))&&ts.isIdentifier(node.expression)&&errorFactories.has(node.expression.text)&&node.arguments?.length) codeLiterals(node.arguments[0]);
|
||||
// Codes picked into a variable first: errCode = "streamer_unreachable"
|
||||
if(ts.isVariableDeclaration(node)&&ts.isIdentifier(node.name)&&/^(errCode|code)$/.test(node.name.text)) codeLiterals(node.initializer);
|
||||
if(ts.isBinaryExpression(node)&&node.operatorToken.kind===ts.SyntaxKind.EqualsToken&&ts.isIdentifier(node.left)&&/^(errCode|code)$/.test(node.left.text)) codeLiterals(node.right);
|
||||
ts.forEachChild(node,visit);
|
||||
}
|
||||
visit(source);
|
||||
@@ -23,7 +40,12 @@ scan('src');
|
||||
const types=['Error','TypeError','RangeError','ReferenceError','SyntaxError','URIError','EvalError','AggregateError','AnonymousError','HTTPError','RequestError','MongoServerError','MongoNetworkError','MongoServerSelectionError','ValidationError','TimeoutError','AbortError','UnknownError'];
|
||||
const vrl=fs.readFileSync('deploy/logging/sanitize.vrl','utf8');
|
||||
const routes=JSON.parse(vrl.match(/if includes\((\[.*?\]), record.route\)/)[1]);
|
||||
fs.writeFileSync('src/core/log-diagnostic-schema.ts','// Generated by scripts/generate-log-diagnostics.js; do not edit.\nexport const LOG_SOURCE_FUNCTIONS: Record<string, readonly string[]> = '+JSON.stringify(files,null,2)+';\nexport const LOG_ERROR_TYPES = '+JSON.stringify(types)+';\nexport const LOG_ROUTES = '+JSON.stringify(routes)+';\n');
|
||||
const codes=[...thrownCodes].sort();
|
||||
fs.writeFileSync('src/core/log-diagnostic-schema.ts','// Generated by scripts/generate-log-diagnostics.js; do not edit.\nexport const LOG_SOURCE_FUNCTIONS: Record<string, readonly string[]> = '+JSON.stringify(files,null,2)+';\nexport const LOG_ERROR_TYPES = '+JSON.stringify(types)+';\nexport const LOG_ROUTES = '+JSON.stringify(routes)+';\nexport const LOG_THROWN_ERROR_CODES = '+JSON.stringify(codes)+';\n');
|
||||
// The collector keeps reviewed codes too (system errnos, worker codes); only add.
|
||||
const codeList=/(if includes\()(\[.*?\])(, record\.errorCode\))/;
|
||||
const collectorCodes=JSON.parse(vrl.match(codeList)[2]);
|
||||
const mergedCodes=[...collectorCodes,...codes.filter(code=>!collectorCodes.includes(code))];
|
||||
const block=`# BEGIN GENERATED DIAGNOSTICS
|
||||
source_functions = ${JSON.stringify(files)}
|
||||
for_each(["errorType", "causeType"]) -> |_index, key| {
|
||||
@@ -43,5 +65,5 @@ for_each(["errorStack", "causeStack"]) -> |_index, key| {
|
||||
}
|
||||
}
|
||||
# END GENERATED DIAGNOSTICS`;
|
||||
const clean=vrl.replace(/# BEGIN GENERATED DIAGNOSTICS[\s\S]*?# END GENERATED DIAGNOSTICS\n?/,'');
|
||||
const clean=vrl.replace(/# BEGIN GENERATED DIAGNOSTICS[\s\S]*?# END GENERATED DIAGNOSTICS\n?/,'').replace(codeList,(_,start,_list,end)=>start+JSON.stringify(mergedCodes).replace(/","/g,'", "')+end);
|
||||
fs.writeFileSync('deploy/logging/sanitize.vrl',clean.replace('out._msg = out.event',()=>block+'\nout._msg = out.event'));
|
||||
Reference in new issue
Block a user