Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 9 additions & 22 deletions lib/utils/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,15 @@ func UserMessageFromError(err error) string {
// The error message is escaped if necessary. A newline is added if the error text
// does not end with a newline.
func FormatErrorWithNewline(err error) string {
message := formatError(err)
var buf bytes.Buffer
formatErrorWriter(err, &buf)
message := buf.String()
if !strings.HasSuffix(message, "\n") {
message = message + "\n"
}
return message
}

// formatError returns user friendly error message from error.
// The error message is escaped if necessary
func formatError(err error) string {
var buf bytes.Buffer
formatErrorWriter(err, &buf)
return buf.String()
}

// formatErrorWriter formats the specified error into the provided writer.
// The error message is escaped if necessary
func formatErrorWriter(err error, w io.Writer) {
Expand All @@ -202,22 +196,15 @@ func formatErrorWriter(err error, w io.Writer) {
fmt.Fprintln(w, certErr)
return
}
// If the error is a trace error, check if it has a user message embedded in
// it, if it does, print it, otherwise escape and print the original error.
if traceErr, ok := err.(*trace.TraceErr); ok {
for _, message := range traceErr.Messages {
fmt.Fprintln(w, AllowWhitespace(message))
}
fmt.Fprintln(w, AllowWhitespace(trace.Unwrap(traceErr).Error()))
return
}
strErr := err.Error()

msg := trace.UserMessage(err)
// Error can be of type trace.proxyError where error message didn't get captured.
if strErr == "" {
if msg == "" {
fmt.Fprintln(w, "please check Teleport's log for more details")
} else {
fmt.Fprintln(w, AllowWhitespace(err.Error()))
return
}

fmt.Fprintln(w, AllowWhitespace(msg))
}

func formatCertError(err error) string {
Expand Down
16 changes: 10 additions & 6 deletions lib/utils/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ package utils
import (
"crypto/x509"
"fmt"
"strings"
"testing"

"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)

func TestUserMessageFromError(t *testing.T) {
t.Parallel()
// Behavior is different in debug
priorLevel := logrus.GetLevel()
logrus.SetLevel(logrus.InfoLevel)
t.Cleanup(func() {
logrus.SetLevel(priorLevel)
})

t.Skip("Enable after https://drone.gravitational.io/gravitational/teleport/3517 is merged.")
tests := []struct {
comment string
inError error
Expand All @@ -47,14 +51,14 @@ func TestUserMessageFromError(t *testing.T) {
},
{
comment: "outputs user message as provided",
inError: trace.Errorf("\x1b[1mWARNING\x1b[0m"),
outString: `error: "\x1b[1mWARNING\x1b[0m"`,
inError: trace.Errorf("bad thing occurred"),
outString: "\x1b[31mERROR: \x1b[0mbad thing occurred",
},
}

for _, tt := range tests {
message := UserMessageFromError(tt.inError)
require.True(t, strings.HasPrefix(message, tt.outString), tt.comment)
require.Contains(t, message, tt.outString)
}
}

Expand Down