我对javascript / nodejs完全陌生,我只是想使用try / catch向脚本添加一些错误处理。
我尝试在google / stackoverflow上查找示例,但我认为我犯了一个愚蠢的语法错误,因为我无法获取它来成功返回catch部分
const IncomingWebhook = require('@slack/webhook').IncomingWebhook; //function grabbed from slack webhook devkit library
var url = process.env.SLACK_WEBHOOK_URL; //process SLACK_WEBHOOK_URL from env variables
var webhook = new IncomingWebhook(url);
const TOTAL_GHE_ISSUES = "10"
const GHE_ISSUE_NUMBERS = "90"
// 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");
}
}
else {
webhook.send({
text: "*Daily Overdue Nessus Vulnerability Alert*",
attachments: [{color: "#36a64f", blocks: [{type: "section",text: {type: "mrkdwn",text: "@here *NO* Overdue Nessus Vulnerabilities reported"}}]}]
});
}
}
else {console.log("No webhook provided");}
尝试可以按预期进行,只要我给它一个有效的webhook,它将成功发布。 catch逻辑每次都会失败(我只希望暂时将其发布到console.log)。如果有人可以帮助我修复语法,我将不胜感激
编辑:我已尝试按照建议将“ .catch(err)”替换为“ catch(error)”,但它返回一个不同的错误“ UnhandledPromiseRejectionWarning:错误:发生了请求错误:connect ECONNREFUSED 127.0.0.1:80” 。我在想webhook.send有一个内置的承诺?我正在使用https://slack.dev/node-slack-sdk/webhook