Skip to content

feat: introduce custom yargs error object#765

Merged
bcoe merged 4 commits into7.xfrom
yerror
Jan 22, 2017
Merged

feat: introduce custom yargs error object#765
bcoe merged 4 commits into7.xfrom
yerror

Conversation

@bcoe
Copy link
Member

@bcoe bcoe commented Jan 21, 2017

BREAKING CHANGE: yargs will no longer aggressively supress errors, allowing errors that are not generated internally to bubble.

CC: @iarna, @satazor curious as to what you think of this implementation.

@bcoe bcoe requested review from maxrimue and nexdrew January 21, 2017 03:47
@bcoe
Copy link
Member Author

bcoe commented Jan 21, 2017

@JaKXz you were mentioning you'd be happy to start helping out on yargs as well? Would love the extra set of 👀 on this.

for (var i = 0, ii = args.length; i < ii; ++i) {
if (handlers[args[i]] && handlers[args[i]].builder) {
return handlers[args[i]].builder(yargs.reset()).argv
const builder = handlers[args[i]].builder
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic was creating an exception that we were quietly ignoring. It came up when the builder was an object rather than a function, or the builder returned nothing -- because this error wasn't a YError tests started failing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this isn't actually related to YError but a bug that was discovered because of it?

describe: 'second option'
}
})
.argv
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test wasn't using the command api appropriately.

lib/yerror.js Outdated
function YError (msg) {
this.name = 'YError'
this.message = msg || 'yargs error'
this.stack = (new Error()).stack
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this last line would be better as:

  Error.captureStackTrace(this, YError)

By using captureStackTrace you won't get the YError constructor in the call stack.

args = parseArgs(processArgs)
} catch (err) {
usage.fail(err.message, err)
if (err instanceof YError) usage.fail(err.message, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so this is the magic moment!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep!

Copy link
Contributor

@iarna iarna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! 👍

Copy link
Member

@satazor satazor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

lib/yerror.js Outdated
@@ -0,0 +1,11 @@
const util = require('util')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a lotta code to do a very little thing… Ben's implementation is 9 lines (a third of which are whitespace) of very simple js, versus … 62 lines. Now concise isn't automatically better, but personally I find what's here way easier to grok. A lot of the weirdness in those 62 lines are about browser compatibility, is that a thing that yargs cares about?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@satazor @iarna hadn't thought of the front-end use-case; I know in the past a couple people had used something like browserify to load yargs in the browser. It might be worth switching from util.inherits to something like:

MyError.prototype = Object.create(Error.prototype);
MyError.prototype.constructor = MyError;

Which I lifted rom the MDN docs.

@satazor to answer your question; I get a lot of pressure from the user-base to keep yargs to as few dependencies as possible:

https://twitter.com/guillaumeQD/status/807917750425440256

More so than a lot of libraries I work on, this has motivated me to opt for less dependencies where possible -- I think people are extra sensitive about library-size, because yargs is something you tack onto a project to add CLI functionality, it's not seen as a core part of the application logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was my only nitpick, thanks for bringing it up @satazor - here's the nodejs discussion on the discouragement of using util.inherits: nodejs/node#4179

args = parseArgs(processArgs)
} catch (err) {
usage.fail(err.message, err)
if (err instanceof YError) usage.fail(err.message, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep!

@bcoe bcoe merged commit 23d00fc into 7.x Jan 22, 2017
@bcoe bcoe deleted the yerror branch January 22, 2017 02:18
bcoe added a commit that referenced this pull request Jan 22, 2017
BREAKING CHANGE: yargs will no longer aggressively supress errors, allowing errors that are not generated internally to bubble.
bcoe added a commit that referenced this pull request Feb 18, 2017
BREAKING CHANGE: yargs will no longer aggressively supress errors, allowing errors that are not generated internally to bubble.
bcoe added a commit that referenced this pull request Feb 26, 2017
BREAKING CHANGE: yargs will no longer aggressively supress errors, allowing errors that are not generated internally to bubble.
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.

4 participants