feat(ai-moderation): implement error handling and robust streaming for llmClient

Wrap the LLM completion logic in a try-catch block to provide detailed
error logging, including status codes and raw response data, when
API requests fail.

- Add comprehensive error logging for failed LLM API calls.
- Ensure streaming responses are correctly aggregated and returned
  even when wrapped in error handling logic.
This commit is contained in:
MythEclipse
2026-06-05 18:47:30 +07:00
parent c0a067322b
commit 049582504e
@@ -100,6 +100,7 @@ export async function llmChat(
return retryWithBackoff( return retryWithBackoff(
async () => { async () => {
return withLlmConcurrency(async () => { return withLlmConcurrency(async () => {
try {
const response = await client.chat.completions.create(params); const response = await client.chat.completions.create(params);
if (stream) { if (stream) {
let content = ""; let content = "";
@@ -140,6 +141,18 @@ export async function llmChat(
} as OpenAI.Chat.Completions.ChatCompletion; } as OpenAI.Chat.Completions.ChatCompletion;
} }
return response as OpenAI.Chat.Completions.ChatCompletion; return response as OpenAI.Chat.Completions.ChatCompletion;
} catch (error: any) {
log.error(
{
error: error.message,
status: error.status,
rawResponse: error.error || error.body || error.response?.data || "N/A",
model
},
"LLM API request failed"
);
throw error;
}
}); });
}, },
{ {