Inquirer.js: Async in validate throws UnhandledPromiseRejectionWarning when validation fails

Created on 5 Sep 2019  ·  10Comments  ·  Source: SBoudrias/Inquirer.js

code:

return inquirer.prompt([
    {
        type: 'input',
        message: 'test',//`Full path to your work directory example. ${process.cwd()}\n`,
        name: 'workdir', 
        /* Legacy way: with this.async */
        validate: function (input) {
            var done = this.async();
            setTimeout(function() {
            if (parseInt(input) == 10) {
                done('You need to provide a number');
                return;
            }
            done(null, true);
            }, 1000);
        }
    }
]).then(answers => {
    console.log(answers);
});

result when answer is 10

(node:97231) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined
    at InputPrompt.onError (/Users/vdj/Sites/installer/node_modules/inquirer/lib/prompts/input.js:95:35)
    at SafeSubscriber._next (/Users/vdj/Sites/installer/node_modules/rxjs/internal/Observable.js:66:21)
    at SafeSubscriber.__tryOrUnsub (/Users/vdj/Sites/installer/node_modules/rxjs/internal/Subscriber.js:205:16)
    at SafeSubscriber.next (/Users/vdj/Sites/installer/node_modules/rxjs/internal/Subscriber.js:143:22)
    at Subscriber._next (/Users/vdj/Sites/installer/node_modules/rxjs/internal/Subscriber.js:89:26)
    at Subscriber.next (/Users/vdj/Sites/installer/node_modules/rxjs/internal/Subscriber.js:66:18)
    at TakeUntilSubscriber.Subscriber._next (/Users/vdj/Sites/installer/node_modules/rxjs/internal/Subscriber.js:89:26)
    at TakeUntilSubscriber.Subscriber.next (/Users/vdj/Sites/installer/node_modules/rxjs/internal/Subscriber.js:66:18)
    at FilterSubscriber._next (/Users/vdj/Sites/installer/node_modules/rxjs/internal/operators/filter.js:52:30)
    at FilterSubscriber.Subscriber.next (/Users/vdj/Sites/installer/node_modules/rxjs/internal/Subscriber.js:66:18)
(node:97231) 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(). (rejection id: 1)
(node:97231) [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.

node -v
v10.16.3

All 10 comments

can confirm ...

(i am using "fs-extra": "^8.1.0")

validate: (inp) => {
        // check if path exists:
        let path = path.join(projects_path, inp);
        // 
        let exists = fse.pathExistsSync(path);
        if(exists) {
            // filepath exists: validation fails:
            return 'A path with that name already exists, cannot proceed.';
        }
        // create Dir:
        fse.ensureDirSync(path);    
        return true;
    },

always throws:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined

        return new Promise( (resolve, reject) => {
            // check if path exists:
            let path = path.join(projects_path, inp);
            let exists = fse.pathExistsSync(path);
            // 
            if(exists) {
                // filepath exists: validation fails:
                reject ('A path with that name already exists, cannot proceed.');
            } else {
                // create Dir:
                fse.ensureDirSync(path);
                resolve (true);
            }
        })

doesn't work either ...

This also happens if an exception is thrown from the validate function. This behavior changed between versions 6.4.1 and 6.5.0. Throwing an exception (and I assume rejecting a promise) used to print the exception message on the command line.

Same error here

Is there any changelog where we can check the changes made?

Same error here , anyone can help?

you can just return the error message。

validate: (input) =>  {
  const pattern = new RegExp("^[a-zA-Z-\/]+$");
  if (!pattern.test(input)) {
    return '格式不正确, 请重新输入';
  }
  return;
}

you can just return the error message。

The problem occurs for async request only so this does not solve the issue

I pushed a fix with [email protected]. Try it out and let me know if the issue persists.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

christav picture christav  ·  5Comments

aaronshaf picture aaronshaf  ·  7Comments

mechanical-turk picture mechanical-turk  ·  3Comments

jahvi picture jahvi  ·  6Comments

fuechter picture fuechter  ·  3Comments