Javascript 如何修复try/catch错误处理中的“未处理的承诺拒绝错误”

Javascript 如何修复try/catch错误处理中的“未处理的承诺拒绝错误”,javascript,node.js,Javascript,Node.js,我试图利用这里的slack devkit将字符串发送到不同的slack通道。我只是尝试在变量webhook失败时包含try/catch错误处理,以发布到静态通道 我已经尝试了try/catch的所有不同变体,但是我无法让任何catch代码正常工作。try逻辑工作正常,符合预期。我是javascript/nodejs新手,所以这可能是我的一个愚蠢错误。我在这个代码段之外定义了SLACK\u WEBHOOK\u URL和SLACK\u MONITORING\u URL const TOTAL_GHE

我试图利用这里的slack devkit将字符串发送到不同的slack通道。我只是尝试在变量webhook失败时包含try/catch错误处理,以发布到静态通道

我已经尝试了try/catch的所有不同变体,但是我无法让任何catch代码正常工作。try逻辑工作正常,符合预期。我是javascript/nodejs新手,所以这可能是我的一个愚蠢错误。我在这个代码段之外定义了SLACK\u WEBHOOK\u URL和SLACK\u MONITORING\u URL

const TOTAL_GHE_ISSUES = "10" //pulled from DB
const GHE_ISSUE_NUMBERS = "90" //pulled from DB

const IncomingWebhook = require('@slack/webhook').IncomingWebhook; //function grabbed from slack webhook devkit library
var url = SLACK_WEBHOOK_URL //can change this to however we want to grab team's webhook
var webhook = new IncomingWebhook(url)

//timer
// Send the notification, if no webhook is present in owners table, skips

if (url != ""){

    if (TOTAL_GHE_ISSUES != "0"){
        try {
        webhook.send({
            text: "*Daily Overdue Nessus Vulnerability Alert*",
            attachments: [{color: "#FF0000", blocks: [{type: "section",text: {type: "mrkdwn",text: "@here *" + TOTAL_GHE_ISSUES + "* Overdue Nessus Vulnerability issues reported \nOverdue Nessus Vulnerability GHE Issue Numbers: *" + GHE_ISSUE_NUMBERS + "*"}}]}]
          })
        }
        catch(err){
            console.log("Webhook Verification Failed")
            //url = SLACK_MONITORING_URL;
            //webhook = new IncomingWebhook(url)
            //webhook.send({
                //text: "*Nessus Webhook Verification, please investigate broken webhooks:*",
                //attachments: [{color: "#FF0000", blocks: [{type: "section",text: {type: "mrkdwn",text: SLACK_WEBHOOK_URL}}]}]
            //})
        }
    }

    else {
        try {
        webhook.send({
            text: "*Daily Overdue Nessus Vulnerability Alert*",
            attachments: [{color: "#36a64f", blocks: [{type: "section",text: {type: "mrkdwn",text: "@here *NO* Overdue Nessus Vulnerabilities reported"}}]}]
          })
      }
        catch(err){
            //url = SLACK_MONITORING_URL
            //webhook = new IncomingWebhook(url)
            //webhook.send({
                //text: "*Nessus Webhook Verification, please investigate broken webhooks:*",
                //attachments: [{color: "#FF0000", blocks: [{type: "section",text: {type: "mrkdwn",text: SLACK_WEBHOOK_URL}}]}]
            //})
        }
  }     
}

else {
  console.log("No webhook provided")}
我希望第一个catcherr发布到console.log,并最终添加更多功能,但我只想要这个基本测试用例,但我得到了这个错误

    at Object.requestErrorWithOriginal (/Users/BrandonKonieczny/Documents/GitHub/node-slack-sdk/node_modules/@slack/webhook/dist/errors.js:25:33)
    at IncomingWebhook.send (/Users/BrandonKonieczny/Documents/GitHub/node-slack-sdk/node_modules/@slack/webhook/dist/IncomingWebhook.js:54:32)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
(node:65139) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:65139) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
在try子句中,webhook.send返回承诺,而不是错误。 返回错误的是承诺本身的结果,因此它不会被catch语句捕获

为了计算它,您可以创建一个异步/等待函数并执行

异步=>{ 试一试{ 等待webhook.send{ 文本:*每日逾期Nessus漏洞警报*, 附件:[{color:FF0000,blocks:[{type:section,text:{type:mrkdwn,text:@here*+TOTAL_GHE_问题+*报告的逾期Nessus漏洞问题\nOverdue Nessus漏洞GHE问题编号:::+GHE_问题编号+*}] } } 捕手{ console.logWebhook验证失败 //url=松弛\u监控\u url; //webhook=新收入WebhookURL //webhook.send{ //文本:*Nessus Webhook验证,请调查损坏的Webhook:, //附件:[{color:FF0000,blocks:[{type:section,text:{type:mrkdwn,text:SLACK\u WEBHOOK\u URL}]}] //} }
}我已尝试按原样集成上述代码,但无论try案例还是catch案例成功,都没有得到明显的结果。当它失败时,它不再发送到slack通道或在命令行上返回任何内容。那里的代码表示一个函数,您必须运行它,因此,您可以声明一个常量const a=async。。。。然后做人工智能。我现在已经可以用了,谢谢你的帮助和耐心!