fix: enhance error logging with additional context in analysis and moderation processes
This commit is contained in:
@@ -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 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -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.`,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user