RN "TypeError: Network request failed" - 生产 - 随机



我知道关于这个主题的一些问题已经被打开了,但我的问题不同:

  • 所有其他的都出现在dev模式中,在我的情况下,它是在生产中
  • 很大比例的请求通过了,其中少数是TypeError: Network request failed,但有时是关键请求
  • 它是随机的,并不总是相同的请求。有时它会过去,有时不会
  • 在我的三个项目中,一个在AWS上,另一个在Clever Cloud上,这两个项目的用户都在1000到5000之间,服务器太大了,我想我消除了服务器故障的风险。即使…当我不在本地启动api时,我也可以在本地进行复制。所以api好像没有响应,但正如我所说,我不这么认为

我不知道该在哪里挖了。。。

我可以把我的API.js服务文件给你,也许你会发现哪里不对劲?


import URI from 'urijs';
import { Platform } from 'react-native';
import NetInfo from '@react-native-community/netinfo';
import { getUserToken, wipeData } from '../utils/data';
import { SCHEME, MW_API_HOST } from '../config';
import deviceInfoModule from 'react-native-device-info';
import { capture } from '../utils/sentry';

const unauthorisedHandler = (navigation) => {
wipeData();
navigation.reset({ index: 0, routes: [{ name: 'Auth' }] });
};
const checkNetwork = async (test = false) => {
const isConnected = await NetInfo.fetch().then((state) => state.isConnected);
if (!isConnected || test) {
await new Promise((res) => setTimeout(res, 1500));
return false;
}
return true;
};
class ApiService {
host = MW_API_HOST;
scheme = SCHEME;
getUrl = (path, query) => {
return new URI().host(this.host).scheme(this.scheme).path(path).setSearch(query).toString();
};
execute = async ({ method = 'GET', path = '', query = {}, headers = {}, body = null }) => {
try {
const config = {
method,
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
appversion: deviceInfoModule.getBuildNumber(),
appdevice: Platform.OS,
currentroute: this.navigation?.getCurrentRoute?.()?.name,
...headers,
},
body: body ? JSON.stringify(body) : null,
};
const url = this.getUrl(path, query);
console.log('url: ', url);
const canFetch = await checkNetwork();
if (!canFetch) return;
let response;
// To try to avoid mysterious `TypeError: Network request failed` error
// that throws an error directly
// we try catch and try one more time.

try {
response = await fetch(url, config);
} catch (e) {
if (e?.toString().includes('Network request failed')) {
// try again
await new Promise((res) => setTimeout(res, 250));
console.log('try again because Network request failed');
response = await fetch(url, config);
} else {
throw e;
}
}
if (!response.ok) {
if (response.status === 401) {
const token = await getUserToken();
if (token) unauthorisedHandler(API.navigation);
return response;
}
}
if (response.json) return await response.json();
return response;
} catch (e) {
capture(e, { extra: { method, path, query, headers, body } });
return { ok: false, error: "Sorry, an error occured, technical team has been warned." };
}
};
executeWithToken = async ({ method = 'GET', path = '', query = {}, headers = {}, body = null }) => {
const token = await getUserToken();
if (token) headers.Authorization = token;
return this.execute({ method, path, query, headers, body });
};
get = async (args) => this.executeWithToken({ method: 'GET', ...args });
post = async (args) => this.executeWithToken({ method: 'POST', ...args });
put = async (args) => this.executeWithToken({ method: 'PUT', ...args });
delete = async (args) => this.executeWithToken({ method: 'DELETE', ...args });
}
const API = new ApiService();
export default API;

与这里和那里的专家交谈,这似乎很正常:互联网网络不是100%可靠的,所以有时,请求会失败,原因是我们无法预料的(隧道,等等(。

我最终使用了fetch-retry,我仍然有一些,但要少得多!

最新更新