Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async in validate throws UnhandledPromiseRejectionWarning when validation fails #845

Closed
kasperkowalski opened this issue Sep 4, 2019 · 10 comments

Comments

@kasperkowalski
Copy link

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

@michapixel
Copy link

can confirm ...

@michapixel
Copy link

(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

@michapixel
Copy link

michapixel commented Sep 9, 2019

		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 ...

@onehorsetown
Copy link

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.

@tiagonapoli
Copy link

Same error here

@tiagonapoli
Copy link

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

@a05151988
Copy link

Same error here , anyone can help?

@ShiChenCong
Copy link

you can just return the error message。

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

@kasperkowalski
Copy link
Author

kasperkowalski commented Nov 7, 2019

you can just return the error message。

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

@SBoudrias
Copy link
Owner

I pushed a fix with Inquirer@7.0.1. Try it out and let me know if the issue persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants