Skip to content

Commit

Permalink
cli/Main: show user details and hints included in error if available
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
knz committed Jul 8, 2019
1 parent f6d75a5 commit 861dc40
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 12 additions & 0 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ func Main() {

errCode := 0
if err := Run(os.Args[1:]); err != nil {
// Display the error and its details/hints.
fmt.Fprintln(stderr, "Error:", err.Error())
maybeShowErrorDetails(stderr, err, false /* printNewline */)

// Remind the user of which command was being run.
fmt.Fprintf(stderr, "Failed running %q\n", cmdName)

// Finally, extract the error code, as optionally specified
// by the sub-command.
errCode = 1
if ec, ok := errors.Cause(err).(*cliError); ok {
errCode = ec.exitCode
Expand Down Expand Up @@ -145,6 +153,10 @@ var cockroachCmd = &cobra.Command{
// Commands should manually print usage information when the error is,
// in fact, a result of a bad invocation, e.g. too many arguments.
SilenceUsage: true,
// Disable automatic printing of the error. We want to also print
// details and hints, which cobra does not do for us. Instead
// we do the printing in Main().
SilenceErrors: true,
}

func init() {
Expand Down
7 changes: 5 additions & 2 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1859,8 +1859,11 @@ Use "cockroach [command] --help" for more information about a command.
done <- err
}()

if err := Run(test.flags); err != nil && !test.expErr {
t.Error(err)
if err := Run(test.flags); err != nil {
fmt.Fprintln(w, "Error:", err)
if !test.expErr {
t.Error(err)
}
}

// back to normal state
Expand Down

0 comments on commit 861dc40

Please sign in to comment.