mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
* feat(cli): add golden tests for errors (#11588) Creates golden files from `coder/cli/errors.go`. Adds a unit test to test against golden files. Adds a make file command to regenerate golden files. Abstracts test against golden files.
This commit is contained in:
+14
-6
@@ -167,9 +167,9 @@ func (r *RootCmd) RunWithSubcommands(subcommands []*serpent.Command) {
|
||||
//nolint:revive
|
||||
os.Exit(code)
|
||||
}
|
||||
f := prettyErrorFormatter{w: os.Stderr, verbose: r.verbose}
|
||||
f := PrettyErrorFormatter{w: os.Stderr, verbose: r.verbose}
|
||||
if err != nil {
|
||||
f.format(err)
|
||||
f.Format(err)
|
||||
}
|
||||
//nolint:revive
|
||||
os.Exit(code)
|
||||
@@ -909,15 +909,23 @@ func ExitError(code int, err error) error {
|
||||
return &exitError{code: code, err: err}
|
||||
}
|
||||
|
||||
type prettyErrorFormatter struct {
|
||||
// NewPrettyErrorFormatter creates a new PrettyErrorFormatter.
|
||||
func NewPrettyErrorFormatter(w io.Writer, verbose bool) *PrettyErrorFormatter {
|
||||
return &PrettyErrorFormatter{
|
||||
w: w,
|
||||
verbose: verbose,
|
||||
}
|
||||
}
|
||||
|
||||
type PrettyErrorFormatter struct {
|
||||
w io.Writer
|
||||
// verbose turns on more detailed error logs, such as stack traces.
|
||||
verbose bool
|
||||
}
|
||||
|
||||
// format formats the error to the console. This error should be human
|
||||
// readable.
|
||||
func (p *prettyErrorFormatter) format(err error) {
|
||||
// Format formats the error to the writer in PrettyErrorFormatter.
|
||||
// This error should be human readable.
|
||||
func (p *PrettyErrorFormatter) Format(err error) {
|
||||
output, _ := cliHumanFormatError("", err, &formatOpts{
|
||||
Verbose: p.verbose,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user