代码之家  ›  专栏  ›  技术社区  ›  hsel

NodeJs+请求承诺-错误捕获

  •  0
  • hsel  · 技术社区  · 7 年前

    我在处理我的机器人程序中的函数时遇到了问题。我现在得到的是一个从网站上删除信息的命令,我想这样做,如果有错误(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;
                    })
            }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   sajan    7 年前

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

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