-
Notifications
You must be signed in to change notification settings - Fork 979
chore: change cli error message handling #9952
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
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f2cbe77
chore: add command to easily visualize different errors
Emyrk 7bf961a
chore: add verbose error printing to cli
Emyrk c7efd8f
Linting
Emyrk 1af63f9
Add validation errors
Emyrk 828737d
Linting
Emyrk 958e2a4
Linting
Emyrk ef67b3d
remove lipgloss
Emyrk 4253615
Lipgloss is now indirect
Emyrk b28c0cc
Linting
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
chore: add command to easily visualize different errors
- Loading branch information
commit f2cbe7760970e2da0827eac0ac82110f1fe72441
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package cli | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/coder/coder/v2/cli/clibase" | ||
"github.com/coder/coder/v2/codersdk" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
func (r *RootCmd) errorExample() *clibase.Cmd { | ||
errorCmd := func(use string, err error) *clibase.Cmd { | ||
return &clibase.Cmd{ | ||
Use: use, | ||
Handler: func(inv *clibase.Invocation) error { | ||
return err | ||
}, | ||
} | ||
} | ||
|
||
cmd := &clibase.Cmd{ | ||
Use: "example-error", | ||
Short: "Shows what different error messages look like", | ||
Long: "This command is pretty pointless, but without it testing errors is" + | ||
"difficult to visually inspect. Error message formatting is inherently" + | ||
"visual, so we need a way to quickly see what they look like.", | ||
Handler: func(inv *clibase.Invocation) error { | ||
return inv.Command.HelpHandler(inv) | ||
}, | ||
Children: []*clibase.Cmd{ | ||
// Typical codersdk error | ||
errorCmd("sdk", &codersdk.Error{ | ||
Response: codersdk.Response{ | ||
Message: "Top level sdk error message.", | ||
Detail: "magic dust unavailable, please try again later", | ||
Validations: []codersdk.ValidationError{ | ||
{ | ||
Field: "region", | ||
Detail: "magic dust is not available in your region", | ||
}, | ||
}, | ||
}, | ||
Helper: "Have you tried turning it off and on again?", | ||
}), | ||
|
||
// Typical cli error | ||
errorCmd("cmd", xerrors.Errorf("some error: %w", errorWithStackTrace())), | ||
|
||
// A multi-error | ||
{ | ||
Use: "multi-error", | ||
Handler: func(inv *clibase.Invocation) error { | ||
// Closing the stdin file descriptor will cause the next close | ||
// to fail. This is joined to the returned Command error. | ||
if f, ok := inv.Stdin.(*os.File); ok { | ||
_ = f.Close() | ||
} | ||
|
||
return xerrors.Errorf("some error: %w", errorWithStackTrace()) | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
type errorClose struct { | ||
io.ReadCloser | ||
} | ||
|
||
func (e errorClose) Close() error { | ||
err := e.ReadCloser.Close() | ||
if err == nil { | ||
return xerrors.Errorf("always close error") | ||
} | ||
return err | ||
} | ||
|
||
func errorWithStackTrace() error { | ||
return xerrors.Errorf("function decided not to work, and it never will") | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Re: lines 103 to 103]
Nifty
See this comment inline on Graphite.