Unexpected token '<', "<!DOCTYPE "... is not valid JSON at JSON.parse (<anonymou...
🚨 错误信息
[09:29:06.645][INFO][ TRSSYz ] [chatgpt][getGeminiKey]随机使用第1个 Key: sk-RipQ****2u2XO7IEmG [09:29:07.907][ERRO][ TRSSYz ] SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON at JSON.parse (<anonymous>) at Response.json (file:///root/TRSS_AllBot/TRSS-Yunzai/node_modules/.pnpm/node-fetch@3.3.2/node_modules/node-fetch/src/body.js:149:15) at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async CustomGoogleGeminiClient.sendMessage (file:///root/TRSS_AllBot/TRSS-Yunzai/plugins/chatgpt-plugin/client/CustomGoogleGeminiClient.js:293:20) at async chatgpt.sendMessage (file:///root/TRSS_AllBot/TRSS-Yunzai/plugins/chatgpt-plugin/model/core.js:653:14) at async chatgpt.abstractChat (file:///root/TRSS_AllBot/TRSS-Yunzai/plugins/chatgpt-plugin/apps/chat.js:900:25) at async chatgpt (file:///root/TRSS_AllBot/TRSS-Yunzai/plugins/chatgpt-plugin/apps/chat.js:580:5) at async PluginsLoader.deal (file:///root/TRSS_AllBot/TRSS-Yunzai/lib/plugins/loader.js:276:45) [09:29:07.910][INFO][2040931135 => 1156231720] 发送群消息:[{"type":"reply","data":{"id":"-438997202"}},{"type":"text","data":{"text":"出现错误:Unexpected token '<', \"<!DOCTYPE \"... is not valid JSON"}}]
🤖 AI解决方案
代码报错分析报告
错误翻译
原文:Unexpected token '<', "<!DOCTYPE "... is not valid JSON
译文:意外的标记'<',"<!DOCTYPE "... 不是有效的JSON格式
原因分析
根本原因:API请求返回了HTML页面而非JSON数据,导致JSON解析失败错误位置:
- 主要错误发生在
CustomGoogleGeminiClient.js第293行的JSON.parse()调用 - API请求地址可能配置错误,返回了HTML错误页面(如404页面)
解决方案
// 在 CustomGoogleGeminiClient.js 中添加错误处理
async sendMessage(message) {
try {
const response = await fetch(apiUrl, options);
// 检查响应内容类型
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.includes('application/json')) {
const textResponse = await response.text();
console.error('API返回非JSON数据:', textResponse.substring(0, 200));
throw new Error('API接口返回异常,请检查API配置');
}
const data = await response.json();
return data;
} catch (error) {
if (error.message.includes('Unexpected token')) {
// 重新获取响应文本分析问题
const textResponse = await response.text();
console.error('API响应内容:', textResponse);
throw new Error(`API接口异常: ${textResponse.split('\n')[0]}`);
}
throw error;
}
}
️ 预防措施
1. API配置检查
2. 防御性编程建议
// 添加响应验证
const validateApiResponse = (response) => {
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const contentType = response.headers.get('content-type');
if (!contentType?.includes('application/json')) {
return response.text().then(text => {
throw new Error(`期望JSON但收到: ${contentType}`);
});
}
return response.json();
};
推荐工具:使用Postman或curl验证API接口是否正常返回JSON数据