json http请求的承诺中的.catch子句的未接收错误
我有此功能从API端点收集JSON数据:
export const getDataAPI = (token_id) => {
const url = `https://api.url.com/tokens/` + token_id;
const options = {method: 'GET', headers: {Accept: 'application/json'}};
const request = fetch(url, options)
.then(response => { if (response.status === 200) {
return response.json();
} else if ([404, 429, 500].includes(response.status)) {
return response.status;
} else {
return response.json()
.then(json => {
console.log("Error: getDataAPI response status", response.status);
throw json;
})
}
})
.catch(error => { throw error; });
return Promise.resolve(request);
};
我已经通过此功能收集了超过130万的记录。我想遇到任何意外的问题,以便我可以优雅地暂停几秒钟,然后重试。但是我遇到了2个错误,这些错误未发送到.CATCH子句。即使响应< / code>为空 / null / undefined,我也希望至少看到
console.log的输出> 这些错误只发生一次。
undefined:1
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Response.json (file:///home/user/node_modules/node-fetch/src/body.js:149:15)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async file:///home/user/path/to/file.mjs:94:20
file:///home/user/node_modules/node-fetch/src/index.js:108
reject(new FetchError(`request to ${request.url} failed, reason: ${error.message}`, 'system', error));
^
FetchError: request to https://api.url.com/tokens/29727608 failed, reason: read ECONNRESET
at ClientRequest.<anonymous> (file:///home/user/node_modules/node-fetch/src/index.js:108:11)
at ClientRequest.emit (node:events:526:28)
at TLSSocket.socketErrorListener (node:_http_client:442:9)
at TLSSocket.emit (node:events:526:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
type: 'system',
errno: 'ECONNRESET',
code: 'ECONNRESET',
erroredSysCall: 'read'
}
我不明白为什么这些错误不会被该功能捕获。有人可以解释一下吗?
如何修复该功能,以便捕获此类错误?
我应该寻找什么输出来识别这些错误,
syntaxerror
和fetcherror
?是否有一个变量可以监视错误代码?
I have this function that collects JSON data from an API endpoint:
export const getDataAPI = (token_id) => {
const url = `https://api.url.com/tokens/` + token_id;
const options = {method: 'GET', headers: {Accept: 'application/json'}};
const request = fetch(url, options)
.then(response => { if (response.status === 200) {
return response.json();
} else if ([404, 429, 500].includes(response.status)) {
return response.status;
} else {
return response.json()
.then(json => {
console.log("Error: getDataAPI response status", response.status);
throw json;
})
}
})
.catch(error => { throw error; });
return Promise.resolve(request);
};
I have collected over 1.3M records with this function. I would like to catch any unexpected problems so I can gracefully pause for a few seconds and try again. But I came across 2 errors that were not sent to the .catch clause. Even if response
is empty / null / undefined, I would expect at least to see the output of console.log("Error: getDataAPI response status", response.status);
These errors only happened once each.
undefined:1
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Response.json (file:///home/user/node_modules/node-fetch/src/body.js:149:15)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async file:///home/user/path/to/file.mjs:94:20
file:///home/user/node_modules/node-fetch/src/index.js:108
reject(new FetchError(`request to ${request.url} failed, reason: ${error.message}`, 'system', error));
^
FetchError: request to https://api.url.com/tokens/29727608 failed, reason: read ECONNRESET
at ClientRequest.<anonymous> (file:///home/user/node_modules/node-fetch/src/index.js:108:11)
at ClientRequest.emit (node:events:526:28)
at TLSSocket.socketErrorListener (node:_http_client:442:9)
at TLSSocket.emit (node:events:526:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
type: 'system',
errno: 'ECONNRESET',
code: 'ECONNRESET',
erroredSysCall: 'read'
}
I don't understand why these errors were not caught by the function. Can someone please explain this?
How can I fix the function so it also catches this sort of errors?
What is the output I should be looking for to identify these errors,
SyntaxError
andFetchError
? Is there a variable to monitor for error codes for example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该代码不在捕获子句中,它在
然后在
子句中。您的
catch
简直就是复发:这将给您带来未知的错误。
您需要记录/处理捕获量中的错误,而不是
然后
子句:That code isn't in a catch clause, it's in a
then
clause.Your
catch
is simply rethrowing:Which will give you an uncaught error.
You need to log/handle the error in the catch, not the
then
clause:在Xdumaine的帮助下,我能够弄清楚自己在做什么错。
这些错误没有触发
console.log
报告,因为它们甚至在可以组成JSON字符串之前就提出了。该功能正在捕获错误,只有对它们没有任何做法。我发现并测试。作为演示,这是一种创建错误并确认.catch()子句处理的方法:
error.name.name
包含错误的名称。With xdumaine's help I was able to figure out what I was doing wrong.
The errors were not triggering the
console.log
reporting because they were raised even before a JSON string could be composed.The function was catching the errors, only there was nothing being done to them. I found this page useful to better understand and test this. As a demonstration, here is a way to create an error and confirm the .catch() clause is handling it:
error.name
contains the name of the error.