Skip to content

feat: improve CLI error messages #6778

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 11 commits into from
Mar 28, 2023
81 changes: 32 additions & 49 deletions cli/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"bufio"
"context"
"errors"
"flag"
Expand All @@ -19,9 +18,7 @@ import (
"strings"
"syscall"
"time"
"unicode/utf8"

"golang.org/x/crypto/ssh/terminal"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -826,74 +823,60 @@ type prettyErrorFormatter struct {
w io.Writer
}

func (prettyErrorFormatter) prefixLines(spaces int, s string) string {
twidth, _, err := terminal.GetSize(0)
if err != nil {
twidth = 80
}

s = lipgloss.NewStyle().Width(twidth - spaces).Render(s)

var b strings.Builder
scanner := bufio.NewScanner(strings.NewReader(s))
for i := 0; scanner.Scan(); i++ {
// The first line is already padded.
if i == 0 {
_, _ = fmt.Fprintf(&b, "%s\n", scanner.Text())
continue
}
_, _ = fmt.Fprintf(&b, "%s%s\n", strings.Repeat(" ", spaces), scanner.Text())
func (p *prettyErrorFormatter) format(err error) {
if err == nil {
return
}
return strings.TrimSuffix(strings.TrimSuffix(b.String(), "\n"), " ")
}

func (p *prettyErrorFormatter) format(err error) {
underErr := errors.Unwrap(err)

arrowStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#515151"))

//nolint:errorlint
if _, ok := err.(*clibase.RunCommandError); ok && p.level == 0 && underErr != nil {
// We can do a better job now.
p.format(underErr)
return
}

var (
padding string
arrowWidth int
)
if p.level > 0 {
const arrow = "┗━ "
arrowWidth = utf8.RuneCount([]byte(arrow))
padding = strings.Repeat(" ", arrowWidth*p.level)
_, _ = fmt.Fprintf(p.w, "%v%v", padding, arrowStyle.Render(arrow))
errorWithoutChildren := err.Error()
if underErr != nil {
errorWithoutChildren = strings.TrimSuffix(err.Error(), ": "+underErr.Error())
}

if underErr != nil {
header := strings.TrimSuffix(err.Error(), ": "+underErr.Error())
_, _ = fmt.Fprintf(p.w, "%s\n", p.prefixLines(len(padding)+arrowWidth, header))
const errorHeader = "ERROR"
// Format the root error specially since it's the most relevant.
if p.level == 0 {
textStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#D16644")).Bold(false)
p.wrappedPrintf(
"%s\n",
fmt.Sprintf(
"%s\n%s",
lipgloss.NewStyle().Inherit(textStyle).Underline(true).Render(errorHeader),
textStyle.Render(errorWithoutChildren),
),
)
p.level++
p.format(underErr)
return
}

{
style := lipgloss.NewStyle().Foreground(lipgloss.Color("#D16644")).Background(lipgloss.Color("#000000")).Bold(false)
// This is the last error in a tree.
textStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#969696")).Bold(false)

border := strings.Repeat("─", len(errorHeader))
p.wrappedPrintf(
"%s\n%s\n",
textStyle.Render(border),
textStyle.Render(errorWithoutChildren),
)
p.level++
if underErr == nil {
// Print closing border.
p.wrappedPrintf(
"%s\n",
p.prefixLines(
len(padding)+arrowWidth,
fmt.Sprintf(
"%s%s%s",
lipgloss.NewStyle().Inherit(style).Underline(true).Render("ERROR"),
lipgloss.NewStyle().Inherit(style).Foreground(arrowStyle.GetForeground()).Render(" ► "),
style.Render(err.Error()),
),
),
textStyle.Render(border),
)
return
}
p.format(underErr)
}

func (p *prettyErrorFormatter) wrappedPrintf(format string, a ...interface{}) {
Expand Down