mirror of
https://github.com/gravitational/teleport.git
synced 2026-09-21 14:35:22 +08:00
Improve utils.FatalError error formatting (#34417)
* Use existing error formatting logic from `trace` * Remove unnecessary layer of indirection * Add test for UserMessageFromError * Fix imports
This commit is contained in:
+10
-23
@@ -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()))
|
||||
|
||||
msg := trace.UserMessage(err)
|
||||
// Error can be of type trace.proxyError where error message didn't get captured.
|
||||
if msg == "" {
|
||||
fmt.Fprintln(w, "please check Teleport's log for more details")
|
||||
return
|
||||
}
|
||||
strErr := err.Error()
|
||||
// Error can be of type trace.proxyError where error message didn't get captured.
|
||||
if strErr == "" {
|
||||
fmt.Fprintln(w, "please check Teleport's log for more details")
|
||||
} else {
|
||||
fmt.Fprintln(w, AllowWhitespace(err.Error()))
|
||||
}
|
||||
|
||||
fmt.Fprintln(w, AllowWhitespace(msg))
|
||||
}
|
||||
|
||||
func formatCertError(err error) string {
|
||||
|
||||
+10
-6
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user