From e91b32df5931f665d066cb3d61f1f451e14d9eae Mon Sep 17 00:00:00 2001 From: k1LoW Date: Mon, 12 Aug 2024 21:23:23 +0900 Subject: [PATCH 1/3] Fix outout of stack traces in text format. --- errors.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/errors.go b/errors.go index 6a79e7f..30a7c0e 100644 --- a/errors.go +++ b/errors.go @@ -105,10 +105,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(fmt.Sprintf("%s", 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() From 97cdc6220e29479aa87ef5ad39a982fea62a5558 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Mon, 12 Aug 2024 21:43:33 +0900 Subject: [PATCH 2/3] bonsai --- errors.go | 5 ++++- errors_test.go | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/errors.go b/errors.go index 30a7c0e..e30219f 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 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) { From 5d684b6d99476d23c43c7673d2c72404869d4487 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Mon, 12 Aug 2024 21:46:52 +0900 Subject: [PATCH 3/3] Fix lint warn --- errors.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/errors.go b/errors.go index e30219f..fbaab82 100644 --- a/errors.go +++ b/errors.go @@ -112,7 +112,7 @@ func (traces stackTraces) String() string { if i > 0 { sb.WriteString("\n") } - sb.WriteString(fmt.Sprintf("%s", errws.Error())) + sb.WriteString(errws.Error()) for _, frame := range errws.Frames { sb.WriteString(fmt.Sprintf("\n%s\n\t%s:%d", frame.Name, frame.File, frame.Line)) }