3

I am facing a very strange issue. As soon as I updated my node to node 16 from 14 my tests stopped working and I get a complain like this:

Error: 
    at /src/api/v1/datastore/SearchesDatastore.ts:109:25
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

And my code the line it shows is:

  public async isRemovalNeeded() {

    try {
      this._logger.debug({message: `Checking if any design doc removal needed.`});
      const designDocsName = getDesignDocLists(this.designDocsPath);
      const allDesignDocs = await this._db.list({
        include_docs: true,
        startkey: "_design/",
        endkey: "_design0"
      });

      const toBeRemoved = allDesignDocs.rows.
        filter((row: any) => !designDocsName.includes(row.id.replace("_design/", "")));
      return toBeRemoved.length > 0;
    } catch (e) {
      this._logger.warn({ message: "Failed to retrieve list of documents." });
      this._logger.debug({ message: `Error: ${e}` });
      throw new Error(errors.ERROR_GETTING_DB_LIST.message);
    }
  }

just to test I ran the same thing with node 14 and it passed with this warning

(node:22352) UnhandledPromiseRejectionWarning: Error: 
    at src/api/v1/datastore/SearchesDatastore.ts:128:19
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:22352) 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: 43)
(node:22352) [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.

so I am thinking it is because of node16 being strict for unhandled promises but in my code I Cannot see any unhandled promise so I am confused

any help is appreciated

Also this is how I call it :

 try {
                    this._loggingService.debug({message: `Checking to see if current pod is a leader`});
                    const isLeader: any = await this._consensusService.isLeader();
                    
                    this._loggingService.debug({message: `current Pod checked for leading and isLeader is: ${isLeader}`});   
                    const isRemovalNeeded = await this._searchesDatastore.isRemovalNeeded();
                    this._loggingService.debug({message: `check occurred to make sure if design doc removal needed and isRemovalNeeded is: ${isRemovalNeeded}`}); 
                    const isUpdateNeeded = await this._searchesDatastore.isUpdateNeeded();
                    this._loggingService.debug({message: `check occurred to make sure if design doc update needed and isUpdateNeeded is: ${isUpdateNeeded}`}); 
                    if (!isRemovalNeeded && !isUpdateNeeded) {
                        shouldWait = false;
                        this._loggingService.info({ message: "All documents are up to date" });
                    } else if (isLeader) {
                        isRemovalNeeded && await this._searchesDatastore.cleanRemovedDesignDocs();
                        isUpdateNeeded && await this._searchesDatastore.syncAllDesignDocs();
                        shouldWait = false;
                    } else {
                        this._loggingService.info({ message: "Design docs are not synced but this pod is not a leader. We need to wait for leader pod to take do the house keeping first." });
                    }
                    if (!shouldWait) {
                        this._loggingService.debug({message: `datastorewatcher is done and we are proceeding to the next step...`});
                        this.watcherFailureCount= 1;
                        resolve(true);
                        break;
                    } else {
                        this._loggingService.debug({message: `datastorewatcher is not done. We will try again after ${config.databaseWatcher.interval}.`})
                        await this.sleep(config.databaseWatcher.interval);
                    }
                } catch (e) {
                    this.watcherFailureCount++;
                    if(this.watcherFailureCount> config.databaseWatcher.toleranceLevel){
                        this._loggingService.warn({message: "App crashed and pod will die soon"})
                        reject(e);
                        break;
                    }
                    const nextIntervalToRun: number= config.databaseWatcher.interval * this.watcherFailureCount;
                    this._loggingService.warn({ message: `DataStoreWatcher failed but still failure is less than tolerance threshold: ${config.databaseWatcher.toleranceLevel}. Watcher will run again in ${nextIntervalToRun}. ${e}` });
                    await this.sleep(nextIntervalToRun);
                }
Learner
  • 1,686
  • 4
  • 19
  • 38
  • How do you invoke `isRemovalNeeded()`? If you throw an `Error` from a method you've marked `async`, it's implicitly returning a `Promise` which will be rejected when it throws an `Error`. If you haven't appropriately caught this rejection at the level above where this code exists you'll notice Node 16 will ungracefully terminate your script. – esqew Jul 06 '22 at 17:35
  • thanks for answering this way const isRemovalNeeded = await this._searchesDatastore.isRemovalNeeded(); – Learner Jul 06 '22 at 17:39
  • Ok, so it doesn't appear you've properly implemented any exception handling in case the Promise returned by `isRemovalNeeded()` is rejected. When an Error is thrown, that's likely the reason your script terminates. Add some exception handling to that call and see if it prevents this premature termination. – esqew Jul 06 '22 at 17:41
  • I added the way I call it in there I do use try catch (please see my updated post) – Learner Jul 06 '22 at 17:47
  • `resolve(true);`, `reject(e);` - that smells like the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! Where are you ultimately catching the rejection? – Bergi Jul 06 '22 at 19:58

0 Answers0