fix: enhance error logging with additional context in analysis and moderation processes

This commit is contained in:
MythEclipse
2026-05-18 23:48:02 +07:00
parent 6339d741a9
commit 069cf105f3
3 changed files with 40 additions and 6 deletions
+13
View File
@@ -95,8 +95,21 @@ async function processAnalysisRequest({
return { ok: true, conversationKey, rows };
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
const errorStack = error instanceof Error ? error.stack : undefined;
const rows: MessageRecord[] = [];
console.error(
JSON.stringify({
level: "ERROR",
context: "aiAnalysisWorker",
conversationKey,
messageCount: messages.length,
error: errorMessage,
stack: errorStack,
timestamp: new Date().toISOString(),
}),
);
return { ok: false, conversationKey, rows, error: errorMessage };
}
}
+18 -2
View File
@@ -102,7 +102,14 @@ async function processBatch(
Date.now() + ERROR_COOLDOWN_MS,
);
logger.error(
{ conversationKey, error: lastError },
{
conversationKey,
error: lastError,
messageCount: messages.length,
messageIds: messages.map((m) => m.id),
cooldownUntil: new Date(Date.now() + ERROR_COOLDOWN_MS).toISOString(),
timestamp: new Date().toISOString(),
},
"Batch analysis failed, will retry after cooldown",
);
return;
@@ -111,12 +118,21 @@ async function processBatch(
conversationErrorCooldown.delete(conversationKey);
} catch (error) {
lastError = error instanceof Error ? error.message : String(error);
const errorStack = error instanceof Error ? error.stack : undefined;
conversationErrorCooldown.set(
conversationKey,
Date.now() + ERROR_COOLDOWN_MS,
);
logger.error(
{ conversationKey, error: lastError },
{
conversationKey,
error: lastError,
stack: errorStack,
messageCount: messages.length,
messageIds: messages.map((m) => m.id),
cooldownUntil: new Date(Date.now() + ERROR_COOLDOWN_MS).toISOString(),
timestamp: new Date().toISOString(),
},
"Analysis worker failed, will retry after cooldown",
);
} finally {
+9 -4
View File
@@ -529,11 +529,16 @@ Return ONLY valid JSON, no other text.`;
try {
parsed = parseModerationResponse(content, targetIds);
} catch (parseError) {
const errorMsg = parseError instanceof Error ? parseError.message : String(parseError);
log.error(
{
error:
parseError instanceof Error ? parseError.message : String(parseError),
content,
error: errorMsg,
contentLength: content.length,
contentPreview: content.substring(0, 500),
fullContent: content,
targetIds,
model: config.AI_LLM_MODEL,
timestamp: new Date().toISOString(),
},
"Robust Fallback: Failed to parse moderation response. Defaulting all targets to clean.",
);
@@ -542,7 +547,7 @@ Return ONLY valid JSON, no other text.`;
status: "clean",
flags: [],
score: 0.1,
analysis: `Parsing failed: ${parseError instanceof Error ? parseError.message : String(parseError)}. Defaulted to clean.`,
analysis: `Parsing failed: ${errorMsg}. Defaulted to clean.`,
}));
}