如果请求由于 NodeJs 中的超时限制而失败,则捕获它



给定以下 nodejs 网页抓取示例,如何确定请求是否由于选项对象中设置的超时限制而失败?

。而不是任何其他原因的结果。

const rp = require('request-promise');
const cheerio = require('cheerio');
const scrape = async (url, keyword) => {
try{
const options = {
uri: url,
timeout: 10000,
transform: function (head) {
return cheerio.load(head);
}
};
const $ = await rp(options);
const links = $('link[rel="stylesheet"]');
const hrefsArr = [];
links.each( (i, el) => { hrefsArr.push(el.attribs.href) });
const result = hrefsArr.some( el => el.includes(keyword) );
return { checkedURL: url, isMatching: result };
}
catch(e){
return { checkedURL: url, isMatching: undefined };
}
};

我的目标是在返回的对象中存储如果请求由于超时而失败。

你可以尝试这样的事情:

const rp = require('request-promise');
var url = 'https://en.wikipedia.org/wiki/42_(number)';
function errorIsDueToTimeout(e) {
if (e.cause && e.cause.code === "ETIMEDOUT")  return true;
if (e.error && e.error.code === "ETIMEDOUT")  return true;
return false;  
}
async function testTimeout() {
try
{
const options = {
uri: url,
timeout: 1 /* We put a really low timeout to test. */
}
var result = await rp(options);
console.log('Retrieved result successfully');
}
catch (e) {
console.error('Error occurred: ' + JSON.stringify(e, null, 2));
if (errorIsDueToTimeout(e)) {
/* Put your timeout handling code here. */
console.log('The error was due to a timeout');
}
return { checkedURL: url, isMatching: undefined, timedOut: errorIsDueToTimeout(e) };
}
};
testTimeout();

最新更新