如何在没有 UnhandledPromiseRejectionWarning 的情况下从 Promise 向调用者抛出异常

时间:2021-03-22 04:39:56

标签: javascript node.js promise try-catch

考虑这个包含 catch 块的数据库查询处理程序:

async function dml(pool, sql, expected = -1) {
  p(sql)
  let rowCnt = await pool.query(sql)
    .then(r => {
      if (expected >= 0 && r.rowCount != expected) {
        throw `DML [${sql}] had wrong number of results: ${r.rowCount} vs expected=${expected}`
      } else {
        return r.rowCount
      }
    })
    .catch(err => {
      msg = `Query [${sql}] failed: ${err}`;
      printError(msg,err)
      throw msg   // THIS is the problem. It generates UnhandledPromiseRejection
    }
  return rowCnt
}

抛出的异常旨在被调用者在这里捕获:

 async function handleClip(data) {
   ..
   // calling code
   try { 
    //  ...
    let cnt = db.dmlClips(sql, 1)   // Throw() happens in this invocation
    debug(`Update count is ${cnt}`)
    return rcode
  } catch (err) {
      //   WHY is the thrown exception not caught here??
    let msg = `Error in handleClip for data=${data.slice(0,min(data.length,200))}`;
    error(msg,err);
  }

但是上面的结构显然是不能接受的:产生如下严重警告:

(node:39959) UnhandledPromiseRejectionWarning: Query [insert into clip ...] failed: error: role "myuser" does not exist
    at emitUnhandledRejectionWarning (internal/process/promises.js:170:15)
    at processPromiseRejections (internal/process/promises.js:247:11)
    at processTicksAndRejections (internal/process/task_queues.js:94:32)
(node:39959) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict`
 (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:39959) [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.
    at emitDeprecationWarning (internal/process/promises.js:180:11)
    at processPromiseRejections (internal/process/promises.js:249:13)
    at processTicksAndRejections (internal/process/task_queues.js:94:32)

那么这需要怎么设置呢?注意这里有一个相关的问题:how to properly throw an error if promise is rejected? (UnhandledPromiseRejectionWarning)。但是对于那个问题,提问者没有任何异常处理程序/捕获块。

2 个答案:

答案 0 :(得分:1)

看起来您从中调用 db.dmlClips 的 try-catch 块不在 async 函数内。在没有 async 关键字的函数中声明的 Try-catch 不会捕获承诺拒绝。

答案 1 :(得分:1)

在调用函数 await 时需要使用 db.dmlClips(sql, 1),以便它等待承诺被解决/拒绝。将该行更改为 let cnt = await db.dmlClips(sql, 1)