HTTP请求错误码429抓不到
HTTP request error code 429 cannot be caught
使用 fetchApi 我正在向端点发出 POST 请求并捕获如下错误:
fetch(new Request(url, {
method: 'POST',
headers: { ...defaultHeaders, ...headers } ,
credentials: 'include',
body: JSON.stringify(body)
})).then(
// handle correct reponse
).catch(
if (err.status === 401) {
// handle error nicely
} else if (err.status === 429) {
// handle error differently
} else {
// handle error yet another way
}
);
虽然 401 错误已按预期处理,但当端点以 429 响应时,代码
else if (err.status === 429) {
// handle error differently
}
从未执行过。
EDIT1:在 429
的情况下,从未达到整个捕获量
javascript/browsers 对 401 和 429 状态码的处理方式不同吗?
如何捕获 429 错误并按我的方式处理?
看起来主要是错误的预期。只有当响应的 type
为 "error"
时,承诺才会被拒绝(因此 catch
被调用),这似乎只适用于非常具体、有限的情况。
Per MDN, the fetch()
API only rejects a promise when a “network error is encountered, although this usually means permissions issues or similar.” Basically fetch()
will only reject a promise if the user is offline, or some unlikely networking error occurs, such a DNS lookup failure.
换句话说,429响应的请求仍然是成功执行的请求。
The good is news is fetch provides a simple ok
flag that indicates whether an HTTP response’s status code is in the successful range or not.
fetch("http://httpstat.us/500")
.then(function(response) {
if (!response.ok) {
throw Error(response.statusText);
}
})
使用 fetchApi 我正在向端点发出 POST 请求并捕获如下错误:
fetch(new Request(url, {
method: 'POST',
headers: { ...defaultHeaders, ...headers } ,
credentials: 'include',
body: JSON.stringify(body)
})).then(
// handle correct reponse
).catch(
if (err.status === 401) {
// handle error nicely
} else if (err.status === 429) {
// handle error differently
} else {
// handle error yet another way
}
);
虽然 401 错误已按预期处理,但当端点以 429 响应时,代码
else if (err.status === 429) {
// handle error differently
}
从未执行过。
EDIT1:在 429
的情况下,从未达到整个捕获量javascript/browsers 对 401 和 429 状态码的处理方式不同吗? 如何捕获 429 错误并按我的方式处理?
看起来主要是错误的预期。只有当响应的 type
为 "error"
时,承诺才会被拒绝(因此 catch
被调用),这似乎只适用于非常具体、有限的情况。
Per MDN, the
fetch()
API only rejects a promise when a “network error is encountered, although this usually means permissions issues or similar.” Basicallyfetch()
will only reject a promise if the user is offline, or some unlikely networking error occurs, such a DNS lookup failure.
换句话说,429响应的请求仍然是成功执行的请求。
The good is news is fetch provides a simple
ok
flag that indicates whether an HTTP response’s status code is in the successful range or not.fetch("http://httpstat.us/500") .then(function(response) { if (!response.ok) { throw Error(response.statusText); } })