NodeJs + Request-promise - 错误捕获

NodeJs + Request-promise - error catching

我在使用我的 Discord 机器人中的函数进行错误处理时遇到了问题。我现在得到的是一个从网站上抓取信息的命令,我想这样做,如果出现错误 (404),用户将得到一些反馈。我该怎么做呢?现在我有一些东西,但我不确定我做错了什么。这是一段代码:

//modules used
const rp = require('request-promise-native');
const errors = require('request-promise/errors');
const cheerio = require('cheerio');

if (message.content.startsWith(prefix + 'latest')) {

    //website url variables
    const website_domain = "https://hypebeast.com/";
    let website_path = args[0];
    let website_url = website_domain + website_path;

    //extra arguments variable
    let extra_arg = args.slice(1).join(" ");

    if (extra_arg.length > 0) {
        message.reply('too many arguments! Please refer to `h.help` for correct usage.');

    } else {

        //opening url and scrapping elements
        function scrapData(website_url) {
            return rp(website_url)
                .then(body => {
                    let items = [],
                        $ = cheerio.load(body).catch(errors.StatusCodeError, function (reason) {
                            console.log(reason);
                        });

                    //web scrapping here
                    $('.post-box').each(function() {
                        let title = $(this).find($('.title h2 span')).first().text(),
                            caption = $(this).find($('.post-box-excerpt p')).first().text(),
                            article_url = $(this).find($('.col-hb-post-image a')).first().attr('href'),
                            thumbnail_long = $(this).find($('.thumbnail img')).first().attr('src');

                        //adding title, caption, etc to list
                        items.push({title, caption, article_url, thumbnail_long});

                        //check items in console
                        console.log(items);
                    })
                    return items;
                })
        }

我刚刚修改了你的代码,试试下面的代码。

//modules used
 const rp = require('request-promise-native');
 const errors = require('request-promise/errors');
 const cheerio = require('cheerio');

 if (message.content.startsWith(prefix + 'latest')) {

//website url variables
const website_domain = "https://hypebeast.com/";
let website_path = args[0];
let website_url = website_domain + website_path;

//extra arguments variable
let extra_arg = args.slice(1).join(" ");

if (extra_arg.length > 0) {
    message.reply('too many arguments! Please refer to `h.help` for correct usage.');

} else {


    var options = {
        uri: website_url,
        transform: function (body) {
            return cheerio.load(body);
        }
    };
    rp(options)
    .then(function ($) {
        // Process html like you would with jQuery... 
        $('.post-box').each(function() {
                    let title = $(this).find($('.title h2 span')).first().text(),
                        caption = $(this).find($('.post-box-excerpt p')).first().text(),
                        article_url = $(this).find($('.col-hb-post-image a')).first().attr('href'),
                        thumbnail_long = $(this).find($('.thumbnail img')).first().attr('src');

                    //adding title, caption, etc to list
                    items.push({title, caption, article_url, thumbnail_long});

                    //check items in console
                    console.log(items);
                });
    })
    .catch(function (err) {
        console.log(err);
    });

}