node.js - NodeJs +请求 promise -错误捕获

标签 node.js error-handling request request-promise

我在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);
    });

}    

关于node.js - NodeJs +请求 promise -错误捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44857548/

相关文章:

c++ - 不是所有的控制路径都返回一个值?警告

c# - 在 C# 中翻译 COM 错误代码

javascript - 如何使用 GraphQL 突变减少请求数?

android usb requestWait不返回

MYSQL concat请求和字符串相减

javascript - NodeJS 使用 module.exports 导出异步变量

node.js - 如何使用 NodeJS 从 EC2 连接和查询 PostgreSQL RDS?

javascript - 如果 URL 中数据库 ID 的变量不存在,Node.js 如何将 URL 重定向到错误

javascript - 如何从 ModelA.js 中调用 ModelB.js 的方法?

返回 NULL 的 PHP 构造函数