Skip to content

Commit

Permalink
Split function into Stack Frame Extraction and Printing
Browse files Browse the repository at this point in the history
  • Loading branch information
kazukousen committed Jan 9, 2024
1 parent 1c37b97 commit df4ffb5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
6 changes: 6 additions & 0 deletions frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import (
"github.com/pkg/errors"
)

type ErrorSet struct {
Error error
Frames Frames
Parent Frames
}

type Frame struct {
File string
Line int
Expand Down
22 changes: 17 additions & 5 deletions pperr.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ func Fprint(w io.Writer, err error) {
}

func FprintFunc(w io.Writer, err error, puts Printer) {
fprintFuncWithParent(w, err, puts, nil)
for _, e := range extractErrorSet(err, nil) {
puts(w, e.Error, e.Frames, e.Parent)
}
}

func ExtractErrorSet(err error) []ErrorSet {
return extractErrorSet(err, nil)
}

func fprintFuncWithParent(w io.Writer, err error, puts Printer, parent Frames) {
func extractErrorSet(err error, parent Frames) []ErrorSet {
if err == nil {
return
return nil
}

realErr := err
Expand All @@ -53,6 +59,8 @@ func fprintFuncWithParent(w io.Writer, err error, puts Printer, parent Frames) {
}
}

var errs []ErrorSet

if withCause, ok := realErr.(interface{ Unwrap() error }); ok {
var causeParent Frames

Expand All @@ -62,10 +70,14 @@ func fprintFuncWithParent(w io.Writer, err error, puts Printer, parent Frames) {
causeParent = parent
}

fprintFuncWithParent(w, withCause.Unwrap(), puts, causeParent)
if es := extractErrorSet(withCause.Unwrap(), causeParent); es != nil {
errs = es
}
}

puts(w, err, frames, parent)
errs = append(errs, ErrorSet{Error: err, Frames: frames, Parent: parent})

return errs
}

func CauseType(err error) string {
Expand Down
45 changes: 45 additions & 0 deletions pperr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,51 @@ func f3(native bool) error {
}
}

func TestExtractErrorSet(t *testing.T) {
assert := assert.New(t)

err := f1(true)

var buf strings.Builder
for _, e := range pperr.ExtractErrorSet(err) {
pperr.DefaultPrinter(&buf, e.Error, e.Frames, e.Parent)
}
actual := buf.String()
actual = regexp.MustCompile(`(?m)[^\s>]+/pperr_test.go:\d+$`).ReplaceAllString(actual, ".../pperr_test.go:NN")
actual = regexp.MustCompile(`(?m)[^\s>]+/go/.*:\d+$`).ReplaceAllString(actual, ".../go/...:NN")
actual = regexp.MustCompile(`(?m):\d+$`).ReplaceAllString(actual, ":NN")

expected := strings.TrimPrefix(`
syscall.Errno: no such file or directory
*fs.PathError: open not_found: no such file or directory
*errors.withStack: from f3(): open not_found: no such file or directory
github.com/kanmu/pperr_test.f3
.../pperr_test.go:NN
github.com/kanmu/pperr_test.f23
.../pperr_test.go:NN
github.com/kanmu/pperr_test.f22
.../pperr_test.go:NN
github.com/kanmu/pperr_test.f21
.../pperr_test.go:NN
*xerrors.wrapError: from f23: from f3(): open not_found: no such file or directory
*fmt.wrapError: from f21(): from f23: from f3(): open not_found: no such file or directory
*errors.withStack: from f2(): from f21(): from f23: from f3(): open not_found: no such file or directory
github.com/kanmu/pperr_test.f2
.../pperr_test.go:NN
*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
.../pperr_test.go:NN
testing.tRunner
.../go/...:NN
runtime.goexit
.../go/...:NN
`, "\n")

assert.Equal(expected, actual)
}

func TestFprint(t *testing.T) {
assert := assert.New(t)

Expand Down

0 comments on commit df4ffb5

Please sign in to comment.