Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove lipgloss
  • Loading branch information
Emyrk committed Sep 29, 2023
commit ef67b3d9b18f6a07a504dd373edf5f6857f9ba2d
3 changes: 3 additions & 0 deletions cli/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ func (RootCmd) errorExample() *clibase.Cmd {
recorder := httptest.NewRecorder()
recorder.WriteHeader(http.StatusBadRequest)
resp := recorder.Result()
_ = resp.Body.Close()
resp.Request, _ = http.NewRequest(http.MethodPost, "http://example.com", nil)
apiError := codersdk.ReadBodyAsError(resp)
//nolint:errorlint
apiError.(*codersdk.Error).Response = codersdk.Response{
Message: "Top level sdk error message.",
Detail: "magic dust unavailable, please try again later",
Expand All @@ -37,6 +39,7 @@ func (RootCmd) errorExample() *clibase.Cmd {
},
},
}
//nolint:errorlint
apiError.(*codersdk.Error).Helper = "Have you tried turning it off and on again?"

// Some flags
Expand Down
37 changes: 14 additions & 23 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"text/tabwriter"
"time"

"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-isatty"
"github.com/mitchellh/go-wordwrap"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -964,15 +963,6 @@ func (p *prettyErrorFormatter) format(err error) {
_, _ = p.w.Write([]byte(output + "\n"))
}

func (p *prettyErrorFormatter) printf(style lipgloss.Style, format string, a ...interface{}) {
s := style.Render(fmt.Sprintf(format, a...))
_, _ = p.w.Write(
[]byte(
s,
),
)
}

type formatOpts struct {
Verbose bool
}
Expand All @@ -986,6 +976,7 @@ func cliHumanFormatError(err error, opts *formatOpts) string {
opts = &formatOpts{}
}

//nolint:errorlint
if multi, ok := err.(interface{ Unwrap() []error }); ok {
multiErrors := multi.Unwrap()
if len(multiErrors) == 1 {
Expand All @@ -1010,10 +1001,10 @@ func cliHumanFormatError(err error, opts *formatOpts) string {
// Default just printing the error. Use +v for verbose to handle stack
// traces of xerrors.
if opts.Verbose {
return headLineStyle().Render(fmt.Sprintf("%+v", err))
return pretty.Sprint(headLineStyle(), fmt.Sprintf("%+v", err))
}

return headLineStyle().Render(fmt.Sprintf("%v", err))
return pretty.Sprint(headLineStyle(), fmt.Sprintf("%v", err))
}

// formatMultiError formats a multi-error. It formats it as a list of errors.
Expand All @@ -1032,7 +1023,7 @@ func formatMultiError(multi []error, opts *formatOpts) string {

// Write errors out
var str strings.Builder
_, _ = str.WriteString(headLineStyle().Render(fmt.Sprintf("%d errors encountered:", len(multi))))
_, _ = str.WriteString(pretty.Sprint(headLineStyle(), fmt.Sprintf("%d errors encountered:", len(multi))))
for i, errStr := range errorStrings {
// Indent each error
errStr = strings.ReplaceAll(errStr, "\n", "\n"+indent)
Expand All @@ -1059,15 +1050,15 @@ func formatMultiError(multi []error, opts *formatOpts) string {
// formatter and add it to cliHumanFormatError function.
func formatRunCommandError(err *clibase.RunCommandError, opts *formatOpts) string {
var str strings.Builder
_, _ = str.WriteString(headLineStyle().Render(fmt.Sprintf("Encountered an error running %q", err.Cmd.FullName())))
_, _ = str.WriteString(pretty.Sprint(headLineStyle(), fmt.Sprintf("Encountered an error running %q", err.Cmd.FullName())))

msgString := fmt.Sprintf("%v", err.Err)
if opts.Verbose {
// '%+v' includes stack traces
msgString = fmt.Sprintf("%+v", err.Err)
}
_, _ = str.WriteString("\n")
_, _ = str.WriteString(tailLineStyle().Render(msgString))
_, _ = str.WriteString(pretty.Sprint(tailLineStyle(), msgString))
return str.String()
}

Expand All @@ -1076,30 +1067,30 @@ func formatRunCommandError(err *clibase.RunCommandError, opts *formatOpts) strin
func formatCoderSDKError(err *codersdk.Error, opts *formatOpts) string {
var str strings.Builder
if opts.Verbose {
_, _ = str.WriteString(headLineStyle().Render(fmt.Sprintf("API request error to \"%s:%s\". Status code %d", err.Method(), err.URL(), err.StatusCode())))
_, _ = str.WriteString(pretty.Sprint(headLineStyle(), fmt.Sprintf("API request error to \"%s:%s\". Status code %d", err.Method(), err.URL(), err.StatusCode())))
_, _ = str.WriteString("\n")
}

_, _ = str.WriteString(headLineStyle().Render(err.Message))
_, _ = str.WriteString(pretty.Sprint(headLineStyle(), err.Message))
if err.Helper != "" {
_, _ = str.WriteString("\n")
_, _ = str.WriteString(tailLineStyle().Render(err.Helper))
_, _ = str.WriteString(pretty.Sprint(tailLineStyle(), err.Helper))
}
// By default we do not show the Detail with the helper.
if opts.Verbose || (err.Helper == "" && err.Detail != "") {
_, _ = str.WriteString("\n")
_, _ = str.WriteString(tailLineStyle().Render(err.Detail))
_, _ = str.WriteString(pretty.Sprint(tailLineStyle(), err.Detail))
}
return str.String()
}

// These styles are arbitrary.
func headLineStyle() lipgloss.Style {
return lipgloss.NewStyle().Foreground(lipgloss.Color("#D16644"))
func headLineStyle() pretty.Style {
return cliui.DefaultStyles.Error
}

func tailLineStyle() lipgloss.Style {
return headLineStyle().Copy().Foreground(lipgloss.Color("#969696"))
func tailLineStyle() pretty.Style {
return pretty.Style{pretty.Nop}
}

//nolint:unused
Expand Down