我正在用Fetch来制作
Cloudflare(代理我的流量)有时会返回429(速率限制)。
返回429时,不包括
Access-Control-Allow-Origin
标题。
所以现在我的
fetch
mode: 'cors'
失败,并抛出一个
TypeError
我怎样才能捕捉到这种情况发生的时间,而不是由于网络错误等其他原因引发的时间?
我的代码如下:
try {
let response = await fetch(uri, config); // this throws
if (!response.ok) { // this line does not run
throw response.statusText;
}
let json = await response.json();
return json;
} catch (e) {
console.log(e.message); // "Failed to fetch"
}
检查
MDN docs
当遇到网络错误或服务器端错误配置CORS时,fetch()承诺将以TypeError拒绝,尽管这通常意味着权限问题或类似问题