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

fix(validation): Use the error as a message when none exists otherwise #1268

Merged
merged 1 commit into from Jan 11, 2019

Conversation

softcraft-development
Copy link
Contributor

While exploring yargs I discovered an edge-case error.

_runValidation assumes that parseErrors is an Error object with a message property. However, it's legitimate in JavaScript to throw arbitrary object. When that object is not an Error, it's most commonly a String, i.e.:

throw 'This is an error.'

See: https://jsfiddle.net/softcraft/gxka09nb/

When _runValidation feeds parseErrors.message to YError, it's undefined, and thus YError generates an error message with the (quite unhelpful) 'yargs error' string, not the value of parseErrors.

This change will allow _runValidation to use the value of parseErrors as a fallback if parseErrors.message doesn't exist. This allows throw "error!" to work successfully.

(I encountered this problem by throwing a String error from a coerce() function.)

@evocateur
Copy link
Contributor

Yikes, that is quite the edge case. I like Nicholas Zakas's explanation of why throwing anything that isn't an Error() object is "suboptimal".

Be that as it may, yargs isn't a linter, so let's make the errors useful in more contexts. 🎉

@evocateur evocateur changed the title use the error as a message when none exists otherwise fix(validation): Use the error as a message when none exists otherwise Jan 11, 2019
@evocateur evocateur merged commit 0510fe6 into yargs:master Jan 11, 2019
@softcraft-development
Copy link
Contributor Author

Agreed on avoiding throwing of non-Errors. I only hit this issue as I was doing some quick & dirty Yargs hacking in my project, and figured that the CLI layer was "thin" enough that I didn't need a full stack trace.

@bcoe
Copy link
Member

bcoe commented Feb 2, 2019

@softcraft-development please try:

npm i yargs@next

It should have your changes.

@softcraft-development
Copy link
Contributor Author

Looks good. Thanks!

@bcoe
Copy link
Member

bcoe commented Feb 12, 2019

this is now available in yargs@13.1.0.

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

Successfully merging this pull request may close these issues.

None yet

3 participants