diff --git a/lib/utils/cli.go b/lib/utils/cli.go index 8735c10a7554c..c7ed8bb77e6a9 100644 --- a/lib/utils/cli.go +++ b/lib/utils/cli.go @@ -178,21 +178,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) { @@ -203,22 +197,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 { diff --git a/lib/utils/cli_test.go b/lib/utils/cli_test.go index 69fa3528c11b6..7bc2fa212723f 100644 --- a/lib/utils/cli_test.go +++ b/lib/utils/cli_test.go @@ -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 @@ -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) } }