From 88cf9b3a9d959597c12a3ef5404a09f84d773355 Mon Sep 17 00:00:00 2001 From: kazukousen Date: Tue, 9 Jan 2024 15:59:30 +0900 Subject: [PATCH 1/2] Add TopFrame function --- frame.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/frame.go b/frame.go index c9f41a9..4a8cd75 100644 --- a/frame.go +++ b/frame.go @@ -6,6 +6,27 @@ import ( "github.com/pkg/errors" ) +type ErrorSets []ErrorSet + +func (es ErrorSets) TopFrame() *Frame { + if len(es) == 0 { + return nil + } else if len(es[0].Frames) == 0 { + return nil + } + + top := es[0].Frames + + if p := es[0].Parent; p != nil { + top = top.Exclude(p) + } + if len(top) == 0 { + return nil + } + + return top[0] +} + type ErrorSet struct { Error error Frames Frames From 0073343493be1f32c5607d5978e559b879460d10 Mon Sep 17 00:00:00 2001 From: kazukousen Date: Tue, 9 Jan 2024 16:03:13 +0900 Subject: [PATCH 2/2] Fix function name --- pperr.go | 10 +++++----- pperr_test.go | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pperr.go b/pperr.go index 21ba22f..c47e6f8 100644 --- a/pperr.go +++ b/pperr.go @@ -34,16 +34,16 @@ func Fprint(w io.Writer, err error) { } func FprintFunc(w io.Writer, err error, puts Printer) { - for _, e := range extractErrorSet(err, nil) { + for _, e := range extractErrorSets(err, nil) { puts(w, e.Error, e.Frames, e.Parent) } } -func ExtractErrorSet(err error) []ErrorSet { - return extractErrorSet(err, nil) +func ExtractErrorSets(err error) ErrorSets { + return extractErrorSets(err, nil) } -func extractErrorSet(err error, parent Frames) []ErrorSet { +func extractErrorSets(err error, parent Frames) []ErrorSet { if err == nil { return nil } @@ -70,7 +70,7 @@ func extractErrorSet(err error, parent Frames) []ErrorSet { causeParent = parent } - if es := extractErrorSet(withCause.Unwrap(), causeParent); es != nil { + if es := extractErrorSets(withCause.Unwrap(), causeParent); es != nil { errs = es } } diff --git a/pperr_test.go b/pperr_test.go index 76f6a31..a3faf8c 100644 --- a/pperr_test.go +++ b/pperr_test.go @@ -42,13 +42,13 @@ func f3(native bool) error { } } -func TestExtractErrorSet(t *testing.T) { +func TestExtractErrorSets(t *testing.T) { assert := assert.New(t) err := f1(true) var buf strings.Builder - for _, e := range pperr.ExtractErrorSet(err) { + for _, e := range pperr.ExtractErrorSets(err) { pperr.DefaultPrinter(&buf, e.Error, e.Frames, e.Parent) } actual := buf.String() @@ -76,7 +76,7 @@ syscall.Errno: no such file or directory *errors.withStack: from f1(): from f2(): from f21(): from f23: from f3(): open not_found: no such file or directory github.com/kanmu/pperr_test.f1 .../pperr_test.go:NN - github.com/kanmu/pperr_test.TestExtractErrorSet + github.com/kanmu/pperr_test.TestExtractErrorSets .../pperr_test.go:NN testing.tRunner .../go/...:NN