diff --git a/errors.go b/errors.go index 6a79e7f..fbaab82 100644 --- a/errors.go +++ b/errors.go @@ -8,7 +8,10 @@ import ( "strings" ) -var _ error = (*errorWithStack)(nil) +var ( + _ error = (*errorWithStack)(nil) + _ fmt.Stringer = (stackTraces)(nil) +) // MaxStackDepth is the maximum depth of the stack trace. var MaxStackDepth = 50 @@ -105,10 +108,13 @@ type Frame struct { func (traces stackTraces) String() string { var sb strings.Builder - for _, errws := range traces { - sb.WriteString(fmt.Sprintf("%s\n", errws.Error())) + for i, errws := range traces { + if i > 0 { + sb.WriteString("\n") + } + sb.WriteString(errws.Error()) for _, frame := range errws.Frames { - sb.WriteString(fmt.Sprintf("%s\n\t%s:%d\n", frame.Name, frame.File, frame.Line)) + sb.WriteString(fmt.Sprintf("\n%s\n\t%s:%d", frame.Name, frame.File, frame.Line)) } } return sb.String() diff --git a/errors_test.go b/errors_test.go index 10b0fa0..c62dd3a 100644 --- a/errors_test.go +++ b/errors_test.go @@ -206,13 +206,16 @@ func TestJSON(t *testing.T) { func TestString(t *testing.T) { err := l() s := errors.StackTraces(err).String() - t.Log(s) if !strings.Contains(s, "error a\n") { t.Error(`"error a\n\t" not found`) } if !strings.Contains(s, ".a\n\t") { t.Error(`".a\n\t" not found`) } + if strings.HasSuffix(s, "\n") { + t.Error(`"\n" found`) + } + t.Log(s) } func TestSlogJSON(t *testing.T) {