This commit is contained in:
董海洋
2026-05-24 14:44:35 +08:00
parent 16fca085c0
commit 66e2f0bcdf
+30 -8
View File
@@ -57,16 +57,28 @@ router.post('/generate-product', async (ctx) => {
temperature: 0.7,
max_tokens: 500
}),
timeout: 60000
timeout: 30000
});
if (!response.ok) {
const errorText = await response.text();
console.error('NVIDIA API Error:', response.status, errorText);
ctx.status = 502;
let errorMsg = 'AI 服务调用失败';
if (response.status === 401) {
errorMsg = 'API Key 无效,请检查密钥配置';
} else if (response.status === 403) {
errorMsg = 'API 调用被拒绝,请检查账户权限';
} else if (response.status === 429) {
errorMsg = 'API 调用次数超限,请稍后重试';
} else if (response.status === 503) {
errorMsg = 'AI 服务暂时不可用,请稍后重试';
}
ctx.status = response.status;
ctx.body = {
code: 502,
message: 'AI 服务调用失败,请稍后重试'
code: response.status,
message: errorMsg
};
return;
}
@@ -88,7 +100,7 @@ router.post('/generate-product', async (ctx) => {
ctx.status = 500;
ctx.body = {
code: 500,
message: '无法解析 AI 响应'
message: '无法解析 AI 响应格式'
};
return;
}
@@ -103,10 +115,20 @@ router.post('/generate-product', async (ctx) => {
} catch (error) {
console.error('生成商品信息失败:', error);
ctx.status = 500;
let errorMsg = '生成失败,请稍后重试';
if (error.message.includes('timeout')) {
errorMsg = 'AI 服务响应超时,请检查网络或稍后重试';
} else if (error.message.includes('ENOTFOUND')) {
errorMsg = '无法连接到 AI 服务,请检查网络设置';
} else if (error.message.includes('ECONNRESET')) {
errorMsg = 'AI 服务连接中断,请稍后重试';
}
ctx.status = 503;
ctx.body = {
code: 500,
message: error.message || '生成失败,请稍后重试'
code: 503,
message: errorMsg
};
}
});