-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add CompactUserMessage() and StackTraceReport() #110
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -88,6 +88,21 @@ type UserMessager interface { | |||||
UserMessage() string | ||||||
} | ||||||
|
||||||
// CompactUserMessager returns a user message associated with the error | ||||||
type CompactUserMessager interface { | ||||||
// CompactUserMessage formats the wraoped error message as a compact go-style | ||||||
// wrapped error message. This format is more user-friendly and contains no | ||||||
// new line nor tab (except the ones already present in the wrapped error). | ||||||
// For example: | ||||||
// | ||||||
// trace.Wrap(trace.Wrap(trace.Errorf("foo"), "bar"), "baz") | ||||||
// | ||||||
// Will produce the following compact error: | ||||||
// | ||||||
// foo: bar: baz | ||||||
CompactUserMessage() string | ||||||
} | ||||||
|
||||||
// ErrorWrapper wraps another error | ||||||
type ErrorWrapper interface { | ||||||
// OrigError returns the wrapped error | ||||||
|
@@ -100,6 +115,11 @@ type DebugReporter interface { | |||||
DebugReport() string | ||||||
} | ||||||
|
||||||
// StackTraceReporter returns the error stacktrace. | ||||||
type StackTraceReporter interface { | ||||||
StackTraceReport() Traces | ||||||
} | ||||||
|
||||||
// UserMessage returns user-friendly part of the error | ||||||
func UserMessage(err error) string { | ||||||
if err == nil { | ||||||
|
@@ -111,6 +131,26 @@ func UserMessage(err error) string { | |||||
return err.Error() | ||||||
} | ||||||
|
||||||
// CompactUserMessage formats the wraoped error message as a compact go-style | ||||||
// wrapped error message. This format is more user-friendly and contains no | ||||||
// new line nor tab (except the ones already present in the wrapped error). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// For example: | ||||||
// | ||||||
// trace.Wrap(trace.Wrap(trace.Errorf("foo"), "bar"), "baz") | ||||||
// | ||||||
// Will produce the following compact error: | ||||||
// | ||||||
// foo: bar: baz | ||||||
func CompactUserMessage(err error) string { | ||||||
if err == nil { | ||||||
return "" | ||||||
} | ||||||
if wrap, ok := err.(CompactUserMessager); ok { | ||||||
return wrap.CompactUserMessage() | ||||||
} | ||||||
return err.Error() | ||||||
} | ||||||
|
||||||
// UserMessageWithFields returns user-friendly error with key-pairs as part of the message | ||||||
func UserMessageWithFields(err error) string { | ||||||
if err == nil { | ||||||
|
@@ -172,6 +212,17 @@ func GetFields(err error) map[string]interface{} { | |||||
return map[string]interface{}{} | ||||||
} | ||||||
|
||||||
// StackTraceReport returns the error stacktrace if it was captured. | ||||||
func StackTraceReport(err error) Traces { | ||||||
if err == nil { | ||||||
return Traces{} | ||||||
} | ||||||
if wrap, ok := err.(StackTraceReporter); ok { | ||||||
return wrap.StackTraceReport() | ||||||
} | ||||||
return Traces{} | ||||||
} | ||||||
|
||||||
// WrapWithMessage wraps the original error into Error and adds user message if any | ||||||
func WrapWithMessage(err error, message interface{}, args ...interface{}) Error { | ||||||
var trace Error | ||||||
|
@@ -374,6 +425,43 @@ func (e *TraceErr) GoString() string { | |||||
return e.DebugReport() | ||||||
} | ||||||
|
||||||
// CompactUserMessage formats the wraoped error message as a compact go-style | ||||||
// wrapped error message. This format is more user-friendly and contains no | ||||||
// new line nor tab (except the ones already present in the wrapped error). | ||||||
// For example: | ||||||
// | ||||||
// trace.Wrap(trace.Wrap(trace.Errorf("foo"), "bar"), "baz") | ||||||
// | ||||||
// Will produce the following compact error: | ||||||
// | ||||||
// foo: bar: baz | ||||||
Comment on lines
+431
to
+437
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imo we should return if i have return trace.Wrap(login(), "logging in") i would want this to be formatted like i'm pretty sure this is actually what the code does, based on the tests, and the comment is just wrong |
||||||
func (e *TraceErr) CompactUserMessage() string { | ||||||
if len(e.Messages) > 0 { | ||||||
sb := strings.Builder{} | ||||||
for i := len(e.Messages) - 1; i >= 0; i-- { | ||||||
sb.WriteString(e.Messages[i]) | ||||||
sb.WriteString(": ") | ||||||
} | ||||||
sb.WriteString(e.Err.Error()) | ||||||
return sb.String() | ||||||
} | ||||||
if e.Message != "" { | ||||||
// For backwards compatibility return the old user message if it's present. | ||||||
return e.Message | ||||||
} | ||||||
return CompactUserMessage(e.Err) | ||||||
} | ||||||
|
||||||
// StackTraceReport returns the error stacktrace. | ||||||
func (e *TraceErr) StackTraceReport() Traces { | ||||||
if len(e.Traces) > 0 { | ||||||
traces := make(Traces, len(e.Traces)) | ||||||
copy(traces, e.Traces) | ||||||
return traces | ||||||
} | ||||||
return StackTraceReport(e.Err) | ||||||
} | ||||||
|
||||||
// maxHops is a max supported nested depth for errors | ||||||
const maxHops = 50 | ||||||
|
||||||
|
@@ -387,6 +475,8 @@ type Error interface { | |||||
ErrorWrapper | ||||||
DebugReporter | ||||||
UserMessager | ||||||
CompactUserMessager | ||||||
StackTraceReporter | ||||||
|
||||||
// GetFields returns any fields that have been added to the error | ||||||
GetFields() map[string]interface{} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,11 @@ import ( | |
func TestEmpty(t *testing.T) { | ||
assert.Equal(t, "", DebugReport(nil)) | ||
assert.Equal(t, "", UserMessage(nil)) | ||
assert.Equal(t, "", CompactUserMessage(nil)) | ||
assert.Equal(t, "", UserMessageWithFields(nil)) | ||
assert.Equal(t, map[string]interface{}{}, GetFields(nil)) | ||
var err TraceErr | ||
assert.Equal(t, Traces{}, err.StackTraceReport()) | ||
} | ||
|
||
func TestWrap(t *testing.T) { | ||
|
@@ -45,7 +48,10 @@ func TestWrap(t *testing.T) { | |
assert.Regexp(t, ".*trace_test.go.*", line(DebugReport(err))) | ||
assert.NotRegexp(t, ".*trace.go.*", line(DebugReport(err))) | ||
assert.NotRegexp(t, ".*trace_test.go.*", line(UserMessage(err))) | ||
assert.NotRegexp(t, ".*trace_test.go.*", line(CompactUserMessage(err))) | ||
assert.Regexp(t, ".*param.*", line(UserMessage(err))) | ||
assert.Regexp(t, ".*param.*", line(CompactUserMessage(err))) | ||
assert.Regexp(t, ".*trace_test.go.*", err.StackTraceReport()[0].Path) | ||
} | ||
|
||
func TestOrigError(t *testing.T) { | ||
|
@@ -67,15 +73,18 @@ func TestWrapUserMessage(t *testing.T) { | |
assert.Regexp(t, ".*trace_test.go.*", line(DebugReport(err))) | ||
assert.NotRegexp(t, ".*trace.go.*", line(DebugReport(err))) | ||
assert.Equal(t, "user message\tdescription", line(UserMessage(err))) | ||
assert.Equal(t, "user message: description", CompactUserMessage(err)) | ||
|
||
err = Wrap(err, "user message 2") | ||
assert.Equal(t, "user message 2\tuser message\t\tdescription", line(UserMessage(err))) | ||
assert.Equal(t, "user message 2: user message: description", CompactUserMessage(err)) | ||
} | ||
|
||
func TestWrapWithMessage(t *testing.T) { | ||
testErr := fmt.Errorf("description") | ||
err := WrapWithMessage(testErr, "user message") | ||
assert.Equal(t, "user message\tdescription", line(UserMessage(err))) | ||
assert.Equal(t, "user message: description", CompactUserMessage(err)) | ||
assert.Regexp(t, ".*trace_test.go.*", line(DebugReport(err))) | ||
assert.NotRegexp(t, ".*trace.go.*", line(DebugReport(err))) | ||
} | ||
|
@@ -106,6 +115,18 @@ func TestGetFields(t *testing.T) { | |
assert.Equal(t, fields, GetFields(e)) | ||
} | ||
|
||
func TestStackTraceReport(t *testing.T) { | ||
testErr := fmt.Errorf("description") | ||
assert.Equal(t, Traces{}, StackTraceReport(testErr)) | ||
|
||
err := Wrap(testErr, "user message") | ||
stackTrace := StackTraceReport(err) | ||
assert.Len(t, stackTrace, 3) | ||
assert.Equal(t, stackTrace[0].Func, "github.com/gravitational/trace.TestStackTraceReport") | ||
assert.Equal(t, stackTrace[1].Func, "testing.tRunner") | ||
assert.Equal(t, stackTrace[2].Func, "runtime.goexit") | ||
Comment on lines
+126
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we avoid asserting on these implementation details? probably best to create a stack trace with a few levels we control and only assert on those |
||
} | ||
|
||
func roundtripError(err error) error { | ||
w := newTestWriter() | ||
WriteError(w, err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.