Skip to content

fix: show --help message for CLI errors, add tests for delete #1403

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

Merged
merged 8 commits into from
May 19, 2022
Prev Previous commit
Next Next commit
refactor(cli): show --help error message on main
This adds an error message which shows when there is an error with any
commands called to improve the UX.
  • Loading branch information
jsjoeio committed May 19, 2022
commit 32cbc855f8cf99697f5f868956db3ea49a87f43c
5 changes: 3 additions & 2 deletions cmd/coder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (

func main() {
dadjoke()
_, err := cli.Root().ExecuteC()
cmd, err := cli.Root().ExecuteC()
if err != nil {
if errors.Is(err, cliui.Canceled) {
os.Exit(1)
}
_, _ = fmt.Fprintln(os.Stderr, cliui.Styles.Error.Render(err.Error()))
helpErrMsg := fmt.Sprintf("Run '%s %s --help' for usage.", cmd.Root().Name(), cmd.Name())
_, _ = fmt.Fprintln(os.Stderr, cliui.Styles.Error.Render(err.Error()+"/n"+helpErrMsg))
Copy link
Member

Choose a reason for hiding this comment

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

Most errors are not usage errors and shouldn't show the usage. Stuff like HTTP errors would trigger this for example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True! @f0ssel mentioned the same thing. Maybe that's why SilenceErrors is set to true?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way we can detect a cobra error like errors.Is(err, cobra.ErrArgs) or something?

Copy link
Contributor Author

@jsjoeio jsjoeio May 19, 2022

Choose a reason for hiding this comment

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

I looked around the docs but didn't find anything. Seems like it just returns a regular error. Maybe someone sees something though.

os.Exit(1)
}
}
Expand Down