簡體   English   中英

如何在try / catch錯誤處理中修復“未處理的promise拒絕錯誤”

[英]How to fix “unhandled promise rejection error” in try/catch error handling

我正在嘗試使用這里的slack devkit( https://slack.dev/node-slack-sdk/webhook )將字符串發送到不同的松弛通道。 我只是嘗試包含try / catch錯誤處理,以便在變量webhook失敗時發布到靜態通道。

我已嘗試過try / catch的所有不同變體,但我無法使任何catch代碼正常工作。 try邏輯工作正常並且符合預期。 我是javascript / nodejs的新手,所以這對我來說可能是一個愚蠢的錯誤。 我已在此代碼段之外定義了SLACK_WEBHOOK_URL和SLACK_MONITORING_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")}

我希望第一個捕獲(錯誤)發布到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返回一個promise,而不是一個錯誤。 返回錯誤的是promise本身的結果,因此它不會被catch語句捕獲。

為了實現它,您可以創建一個異步/等待函數並執行...

 async () => { try { await 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}}]}] //}) } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM