- Notifications
You must be signed in to change notification settings - Fork1k
feat: add verbose error messaging#3053
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
72ae0ca
eb57e7b
1d65a42
b9d8afc
ae551a8
e085a38
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -4,7 +4,6 @@ import ( | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"text/template" | ||
"time" | ||
@@ -40,11 +39,12 @@ const ( | ||
varAgentURL = "agent-url" | ||
varGlobalConfig = "global-config" | ||
varNoOpen = "no-open" | ||
varNoVersionCheck = "no-version-warning" | ||
varForceTty = "force-tty" | ||
varVerbose = "verbose" | ||
notLoggedInMessage = "You are not logged in. Try logging in using 'coder login <url>'." | ||
envNoVersionCheck = "CODER_NO_VERSION_WARNING" | ||
) | ||
var ( | ||
@@ -58,8 +58,6 @@ func init() { | ||
} | ||
func Root() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "coder", | ||
SilenceErrors: true, | ||
@@ -68,7 +66,7 @@ func Root() *cobra.Command { | ||
`, | ||
PersistentPreRun: func(cmd *cobra.Command, args []string) { | ||
err := func() error { | ||
ifcliflag.IsSetBool(cmd, varNoVersionCheck) { | ||
return nil | ||
} | ||
@@ -141,7 +139,7 @@ func Root() *cobra.Command { | ||
cmd.SetUsageTemplate(usageTemplate()) | ||
cmd.PersistentFlags().String(varURL, "", "Specify the URL to your deployment.") | ||
cliflag.Bool(cmd.PersistentFlags(),varNoVersionCheck, "", envNoVersionCheck, false, "Suppress warning when client and server versions do not match.") | ||
cliflag.String(cmd.PersistentFlags(), varToken, "", envSessionToken, "", fmt.Sprintf("Specify an authentication token. For security reasons setting %s is preferred.", envSessionToken)) | ||
cliflag.String(cmd.PersistentFlags(), varAgentToken, "", "CODER_AGENT_TOKEN", "", "Specify an agent authentication token.") | ||
_ = cmd.PersistentFlags().MarkHidden(varAgentToken) | ||
@@ -152,6 +150,7 @@ func Root() *cobra.Command { | ||
_ = cmd.PersistentFlags().MarkHidden(varForceTty) | ||
cmd.PersistentFlags().Bool(varNoOpen, false, "Block automatically opening URLs in the browser.") | ||
_ = cmd.PersistentFlags().MarkHidden(varNoOpen) | ||
cliflag.Bool(cmd.PersistentFlags(), varVerbose, "v", "CODER_VERBOSE", false, "Enable verbose output") | ||
return cmd | ||
} | ||
@@ -427,12 +426,29 @@ func formatExamples(examples ...example) string { | ||
// FormatCobraError colorizes and adds "--help" docs to cobra commands. | ||
func FormatCobraError(err error, cmd *cobra.Command) string { | ||
helpErrMsg := fmt.Sprintf("Run '%s --help' for usage.", cmd.CommandPath()) | ||
var ( | ||
httpErr *codersdk.Error | ||
output strings.Builder | ||
) | ||
if xerrors.As(err, &httpErr) { | ||
_, _ = fmt.Fprintln(&output, httpErr.Friendly()) | ||
} | ||
// If the httpErr is nil then we just have a regular error in which | ||
// case we want to print out what's happening. | ||
if httpErr == nil || cliflag.IsSetBool(cmd, varVerbose) { | ||
_, _ = fmt.Fprintln(&output, err.Error()) | ||
} | ||
_, _ = fmt.Fprint(&output, helpErrMsg) | ||
return cliui.Styles.Error.Render(output.String()) | ||
} | ||
func checkVersions(cmd *cobra.Command, client *codersdk.Client) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Happened to notice this function still uses hard-coded version of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Good catch, we don'tneed to keep both checks but I kept the check at the top of the the pre-run to avoid the unnecessary I/O of instantiating a client that we immediately throw away when someone passes the disable-warning flag. | ||
if cliflag.IsSetBool(cmd, varNoVersionCheck) { | ||
return nil | ||
} | ||