0

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

...而不是由于任何其他原因。

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 };
}
};

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

4

1 回答 1

0

你可以尝试这样的事情:

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();
于 2018-06-20T09:56:43.683 回答