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 }; return { ok: true, conversationKey, rows };
} catch (error) { } catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error); const errorMessage = error instanceof Error ? error.message : String(error);
const errorStack = error instanceof Error ? error.stack : undefined;
const rows: MessageRecord[] = []; 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 }; return { ok: false, conversationKey, rows, error: errorMessage };
} }
} }
+18 -2
View File
@@ -102,7 +102,14 @@ async function processBatch(
Date.now() + ERROR_COOLDOWN_MS, Date.now() + ERROR_COOLDOWN_MS,
); );
logger.error( 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", "Batch analysis failed, will retry after cooldown",
); );
return; return;
@@ -111,12 +118,21 @@ async function processBatch(
conversationErrorCooldown.delete(conversationKey); conversationErrorCooldown.delete(conversationKey);
} catch (error) { } catch (error) {
lastError = error instanceof Error ? error.message : String(error); lastError = error instanceof Error ? error.message : String(error);
const errorStack = error instanceof Error ? error.stack : undefined;
conversationErrorCooldown.set( conversationErrorCooldown.set(
conversationKey, conversationKey,
Date.now() + ERROR_COOLDOWN_MS, Date.now() + ERROR_COOLDOWN_MS,
); );
logger.error( 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", "Analysis worker failed, will retry after cooldown",
); );
} finally { } finally {
+9 -4
View File
@@ -529,11 +529,16 @@ Return ONLY valid JSON, no other text.`;
try { try {
parsed = parseModerationResponse(content, targetIds); parsed = parseModerationResponse(content, targetIds);
} catch (parseError) { } catch (parseError) {
const errorMsg = parseError instanceof Error ? parseError.message : String(parseError);
log.error( log.error(
{ {
error: error: errorMsg,
parseError instanceof Error ? parseError.message : String(parseError), contentLength: content.length,
content, 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.", "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", status: "clean",
flags: [], flags: [],
score: 0.1, score: 0.1,
analysis: `Parsing failed: ${parseError instanceof Error ? parseError.message : String(parseError)}. Defaulted to clean.`, analysis: `Parsing failed: ${errorMsg}. Defaulted to clean.`,
})); }));
} }