我有這個從 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 子句的錯誤。即使response是空/空/未定義,我也希望至少看到console.log("Error: getDataAPI response status", response.status);
這些錯誤的輸出每個只發生一次。
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?例如,是否有用于監視錯誤代碼的變數?
uj5u.com熱心網友回復:
我希望至少能看到 console.log("Error: getDataAPI response status", response.status); 的輸出。
該代碼不在 catch 子句中,而是在then子句中。
return response.json()
.then(json => {
console.log("Error: getDataAPI response status", response.status);
throw json;
})
你catch只是重新拋出:
.catch(error => { throw error; });
這會給你一個未捕獲的錯誤。
您需要記錄/處理 catch 中的錯誤,而不是then子句:
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 {
console.log("Error: getDataAPI response status", response.status);
// you can swallow the error here if you want, or try to parse it anyway
return response.json()
}
})
.catch(error => {
console.log('Handling error', error);
throw error;
});
// wrapping the request in a Promise.resolve is also unnecessary
// return Promise.resolve(request);
return request;
};
uj5u.com熱心網友回復:
我不確定,但您確定 API 回傳格式正確的 JSON 嗎?
或者您可以嘗試捕獲回傳 response.json() 嗎?
uj5u.com熱心網友回復:
是的,我沒有看到,.then 應該放在不同的地方..
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);
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/468128.html
