You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复JavaScript中try/catch语法错误及Promise未捕获问题

解决你的JavaScript/Node.js try/catch与Promise错误处理问题

我来帮你一步步搞定这两个问题!你遇到的第一个是基础语法错误,第二个是异步Promise的错误捕获问题,咱们逐个解决:

1. 先修复SyntaxError: Missing catch or finally after try

你代码里的} .catch(err){写法错了——多了个不必要的点号,同步try/catch的正确语法是catch(err) {,把那个点删掉就搞定这个语法错误了。

2. 解决UnhandledPromiseRejectionWarning

你猜的没错,webhook.send()返回的是Promise,同步的try/catch抓不到异步Promise的rejection错误。要处理这种情况,有两种常用方案:

方案一:用async/await配合try/catch(更直观,推荐)

把代码放到异步函数里,用await等待Promise完成,这样try/catch就能正常捕获错误了,同时别忘了给else分支的webhook.send()也加上错误处理:

const IncomingWebhook = require('@slack/webhook').IncomingWebhook;
var url = process.env.SLACK_WEBHOOK_URL;
var webhook = new IncomingWebhook(url);
const TOTAL_GHE_ISSUES = "10"
const GHE_ISSUE_NUMBERS = "90"

// 用自执行异步函数包裹,支持await语法
(async () => {
  // Send the notification, if no webhook is present in owners table, skips
  if (url != ""){
    if (TOTAL_GHE_ISSUES != "0"){
      try {
        // await等待Promise完成,让异步操作"同步化"
        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) { // 修正后的catch语法
        console.error("Webhook Verification Failed:", err); // 打印错误详情,方便排查
      }
    } else {
      try {
        await 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) {
        console.error("Webhook send failed (no overdue case):", err);
      }
    }
  } else {
    console.log("No webhook provided");
  }
})();

方案二:直接用Promise的.catch()链式调用

如果你不习惯async/await,也可以直接在webhook.send()后面链式调用.catch()来捕获错误:

const IncomingWebhook = require('@slack/webhook').IncomingWebhook;
var url = process.env.SLACK_WEBHOOK_URL;
var webhook = new IncomingWebhook(url);
const TOTAL_GHE_ISSUES = "10"
const GHE_ISSUE_NUMBERS = "90"

if (url != ""){
  if (TOTAL_GHE_ISSUES != "0"){
    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 => { // 链式处理Promise错误
      console.error("Webhook Verification Failed:", err);
    });
  } 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"
          }
        }]
      }]
    })
    .catch(err => {
      console.error("Webhook send failed (no overdue case):", err);
    });
  }
} else {
  console.log("No webhook provided");
}

关键总结

  • 同步try/catch的语法要准确:try {...} catch(err) {...},不要多加点号
  • 异步Promise的错误必须用async/await + try/catch,或者Promise链式.catch()来捕获,同步try/catch抓不到异步错误
  • 所有返回Promise的操作都要加错误处理,避免出现未捕获的Promise警告

内容的提问来源于stack exchange,提问作者Brandon Konieczny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:02:00