Skip to content

Explicitly return from error methods#444

Closed
plemarquand wants to merge 1 commit intotj:masterfrom
plemarquand:master
Closed

Explicitly return from error methods#444
plemarquand wants to merge 1 commit intotj:masterfrom
plemarquand:master

Conversation

@plemarquand
Copy link
Copy Markdown

Instead of relying on process.exit() to terminate execution, also explicitly
return from methods when errors are encountered. This is more flexible for
clients who which to override the methods that use process.exit() in order
to use commander in other contexts than the shell

Fixes #443

@plemarquand plemarquand force-pushed the master branch 2 times, most recently from de167e3 to c0147e1 Compare October 1, 2015 14:56
Instead of relying on process.exit() to terminate execution, also explicitly
return from methods when errors are encountered. This is more flexible for
clients who which to override the methods that use process.exit() in order
to use commander in other contexts than the shell
@zhiyelee
Copy link
Copy Markdown
Collaborator

zhiyelee commented Oct 1, 2015

This will break the old manner?

@plemarquand
Copy link
Copy Markdown
Author

@zhiyelee No, this only comes in to play when you override process.exit, or the commander methods that use it. Commander relies on process.exit to halt program flow. These returns mimic that if process.exit doesn't do it.

By doing so now you can use commander in contexts other than a shell.

@plemarquand
Copy link
Copy Markdown
Author

@SomeKittens @zhiyelee Any thoughts? The patch has some added tests.

@SomeKittens
Copy link
Copy Markdown
Collaborator

@zhiyelee what will break? This looks good to me.

@zhiyelee
Copy link
Copy Markdown
Collaborator

zhiyelee commented Oct 6, 2015

@SomeKittens Nothing break. But I'm not a big fan of this. Since to use this, user must override process.exit first. Not very graceful, but I have no better solution.
Besides I think Commander is designed to use in command line instead as a class can be extended.

@plemarquand
Copy link
Copy Markdown
Author

@zhiyelee I agree its not the most graceful approach when it comes to overriding process.exit. A better one long term would be centralizing all the process.exits into a single method that can then be overridden, and passing to that method the exit code as well as error message. The default approach, as it is now, would be to print that message and exit. However then if you wanted to do something else like forward the message to a remote client, you could do so.

Even if we do that, we'll still need to implement these changes where program flow is interrupted by returns instead of relying on process.exit to do it for us.

@plemarquand
Copy link
Copy Markdown
Author

I should also mention this patch goes most of the way towards fixing the second Con on this list: http://trentm.com/2014/01/a-critique-of-commander-for-nodejs.html

@inikulin
Copy link
Copy Markdown

inikulin commented Oct 8, 2015

In addition to the reason of this PR (testing purposes) we have a requirement to apply custom styling for the error messages to make them consistent across our app (e.g. we are using bold red ERROR prefix). I think the best solution for the both issues will be Command's error event. If it hasn't listeners it will behave as it does currently. If it has listener, when process will not exit and message text is passed as event argument. I can issue PR if you are OK with this behavior.

@kevinoid
Copy link
Copy Markdown

I'd also like to see this (and preferably a way to pass an exit function to the Command constructor). I'm currently overriding process.exit to throw an exception, which also works, but it would be nice to avoid patching process.exit altogether.

@shadowspawn shadowspawn mentioned this pull request Apr 1, 2019
@shadowspawn
Copy link
Copy Markdown
Collaborator

I have added this pull request to a triage collection of exit related issues for future reference.

This pull request has not had any activity in over six months. It isn't likely to get acted on due to this report.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

Thank you for your contributions.

@shadowspawn shadowspawn closed this Apr 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants