错误消息: fetch failed: java.net.SocketException: Software caused connection abort 堆...
错误信息
错误名称: AI_ProviderSpecificError
错误消息: fetch failed: java.net.SocketException: Software caused connection abort
堆栈跟踪: AI_ProviderSpecificError: fetch failed: java.net.SocketException: Software caused connection abort
at construct (native)
at _callSuper (address at index.android.bundle:1:278433)
at _AISDKError (address at index.android.bundle:1:5445613)
at construct (native)
at callSuper (address at index.android.bundle:1:278433)
at ProviderSpecificError (address at index.android.bundle:1:5465926)
at convertAndEmitChunk (address at index.android.bundle:1:5462714)
at ?anon_0 (address at index.android.bundle:1:5461992)
at next (native)
at asyncGeneratorStep (address at index.android.bundle:1:844874)
at _next (address at index.android.bundle:1:845131)
at tryCallOne (address at InternalBytecode.js:1:1180)
at anonymous (address at InternalBytecode.js:1:1874)
原因: {
“cause”: “[undefined]”
}
AI 解决方案
报错分析报告
1.错误翻译
原文:fetch failed: java.net.SocketException: Software caused connection abort
译文:获取失败:java.net.SocketException:软件导致连接中止
2.原因分析
fetch)时网络连接被软件强制中止(SocketException)。fetch操作,堆栈指向convertAndEmitChunk等异步数据处理环节。3.解决方案
// 修复后的代码:带重试+超时的fetch请求(适配React Native/Web)
async function fetchWithRetry(url, options = {}, retries = 3, timeout = 10000) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, { ...options, signal: controller.signal });
clearTimeout(timeoutId);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return response;
} catch (error) {
clearTimeout(timeoutId);
if (retries > 0 && error.name !== 'AbortError') {
await new Promise(resolve => setTimeout(resolve, 1000 * (4 - retries))); // 指数退避
return fetchWithRetry(url, options, retries - 1, timeout);
}
throw error;
}
}
// 使用示例(替换YOUR_AI_API_URL为实际地址)
fetchWithRetry('YOUR_AI_API_URL', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prompt: 'your input' })
})
.then(res => res.json())
.catch(err => console.error('最终错误:', err));
4.️ 预防措施
react-native-netinfo检查网络状态。axios替代原生fetch(内置拦截器/超时管理更便捷)。axios(HTTP客户端)、react-native-netinfo(网络状态检测)、winston(错误日志)。