WestCoastProjects
WestCoastProjects

Reputation: 63201

How to throw an exception from a Promise to the Caller without an UnhandledPromiseRejectionWarning

Consider this database query handler that does contain a catch block:

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
}

The thrown exception is intended to be caught by the caller here:

 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);
  }

But the above structure is not acceptable apparently: the following serious warning is generated:

(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)

So how does this need to be set up ? Note there is a related question here: how to properly throw an error if promise is rejected? (UnhandledPromiseRejectionWarning) . But for that question the asker did not have any exception handler/catch block.

Upvotes: 0

Views: 376

Answers (2)

Rifat Bin Reza
Rifat Bin Reza

Reputation: 2781

You need to use await when you're calling the function db.dmlClips(sql, 1) so that it waits for the promise to be resolved/rejected. Change the line to let cnt = await db.dmlClips(sql, 1).

Upvotes: 1

Denis Malykhin
Denis Malykhin

Reputation: 347

It looks like the try-catch block from which you are invoking db.dmlClips isn't inside an async function. Try-catch declared in functions without async keywords won't catch a promise rejection.

Upvotes: 1

Related Questions