如果由于 NodeJs 中的超时限制导致请求不成功,则捕获该请求
Catching that if the request was unsuccessful because of timeout limit in 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();
给定以下 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();