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

Harmonize BeforeError handling #1117

Merged
merged 2 commits into from Apr 28, 2020

Conversation

creekorful
Copy link
Contributor

@creekorful creekorful commented Apr 27, 2020

What type of PR is this?

  • bug
  • cleanup
  • documentation
  • feature

What this PR does / why we need it:

The error handling using BeforeFunc is now harmonized between App#RunContext, App#RunAsSubcommand & Command#Run.

Which issue(s) this PR fixes:

Fixes #1081

Release Notes

(REQUIRED)

Error handling using BeforeFunc is now harmonized between App#RunContext, App#RunAsSubcommand and Command#Run.

@creekorful creekorful requested a review from a team as a code owner April 27, 2020 17:54
@creekorful creekorful requested review from coilysiren and asahasrabuddhe and removed request for a team April 27, 2020 17:54
@rliebz
Copy link
Member

rliebz commented Apr 28, 2020

Thanks for following up! This is great.

I did a quick audit of the code base, it looks like there's one more BeforeFunc that prints command help in case of errors:

cli/command.go

Lines 150 to 157 in 4bb97e0

if c.Before != nil {
err = c.Before(context)
if err != nil {
_ = ShowCommandHelp(context, c.Name)
context.App.handleExitCoder(context, err)
return err
}
}

Would you mind dropping the call to ShowCommandHelp there as well for consistency?

@creekorful
Copy link
Contributor Author

Thanks for following up! This is great.

I did a quick audit of the code base, it looks like there's one more BeforeFunc that prints command help in case of errors:

cli/command.go

Lines 150 to 157 in 4bb97e0

if c.Before != nil {
err = c.Before(context)
if err != nil {
_ = ShowCommandHelp(context, c.Name)
context.App.handleExitCoder(context, err)
return err
}
}

Would you mind dropping the call to ShowCommandHelp there as well for consistency?

Yup, my bad should have seen this one.

Done !

@creekorful
Copy link
Contributor Author

Btw is it wanted that this logic is uncoupled and not centralised?

IMHO since the BeforeError handling is common (and this is wanted) between the 3 occurrences, I would have centralise the logic somewhere

@rliebz rliebz merged commit 464c868 into urfave:master Apr 28, 2020
@rliebz
Copy link
Member

rliebz commented Apr 28, 2020

There's a lot of logic like this that I'd like to see centralized. There are subtle reasons why it's not that way right now, such as repeated logic for different types or handling named return parameters. Not that it can't be improved—it absolutely can be. But it's not necessarily easy.

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.

v2 feature: Make error message optional in BeforeFunc
3 participants