Skip to content

Commit c9e31bb

Browse files
committed
Remove unnecessary recursion
1 parent aaf95d9 commit c9e31bb

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

cli/root.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -818,57 +818,57 @@ func isConnectionError(err error) bool {
818818
}
819819

820820
type prettyErrorFormatter struct {
821-
level int
822-
w io.Writer
821+
w io.Writer
823822
}
824823

825824
func (p *prettyErrorFormatter) format(err error) {
826-
if err == nil {
827-
// 🏁
828-
p.printf("\n")
829-
return
830-
}
831-
832-
nextErr := errors.Unwrap(err)
825+
errTail := errors.Unwrap(err)
833826

834827
//nolint:errorlint
835-
if _, ok := err.(*clibase.RunCommandError); ok && p.level == 0 && nextErr != nil {
828+
if _, ok := err.(*clibase.RunCommandError); ok && errTail != nil {
836829
// Avoid extra nesting.
837-
p.format(nextErr)
830+
p.format(errTail)
838831
return
839832
}
840833

841834
var headErr string
842-
if nextErr != nil {
843-
headErr = strings.TrimSuffix(err.Error(), ": "+nextErr.Error())
835+
if errTail != nil {
836+
headErr = strings.TrimSuffix(err.Error(), ": "+errTail.Error())
844837
} else {
845838
headErr = err.Error()
846839
}
847840

848841
var msg string
849842
var sdkError *codersdk.Error
850843
if errors.As(err, &sdkError) {
851-
msg = sdkError.Message + sdkError.Helper
844+
// We don't want to repeat the same error message twice, so we
845+
// only show the SDK error on the top of the stack.
846+
msg = sdkError.Message
847+
if sdkError.Helper != "" {
848+
msg = msg + "\n" + sdkError.Helper
849+
}
850+
// The SDK error is usually good enough, and we don't want to overwhelm
851+
// the user with output.
852+
errTail = nil
852853
} else {
853854
msg = headErr
854855
}
855856

856857
textStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#D16644"))
857-
if p.level > 0 {
858-
// Grey out the less important, deep errors.
859-
textStyle.Foreground(lipgloss.Color("#969696"))
860-
}
861-
862-
if nextErr != nil {
858+
if errTail != nil {
863859
msg = msg + ": "
864860
}
865-
866861
p.printf(
867862
"%s",
868863
textStyle.Render(msg),
869864
)
870-
p.level++
871-
p.format(nextErr)
865+
866+
if errTail != nil {
867+
// Grey out the less important, deep errors.
868+
textStyle.Foreground(lipgloss.Color("#969696"))
869+
p.printf("%s", textStyle.Render(errTail.Error()))
870+
}
871+
p.printf("\n")
872872
}
873873

874874
func (p *prettyErrorFormatter) printf(format string, a ...interface{}) {

0 commit comments

Comments
 (0)