Skip to content
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

Split function into Stack Frame Extraction & Printing #9

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion 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 Expand Up @@ -36,7 +42,7 @@ func ExtractFrames(st errors.StackTrace) Frames {
}

func (frames Frames) Exclude(excludes Frames) Frames {
newFrames := Frames{}
newFrames := make(Frames, 0, len(frames))

L1:
for _, f := range frames {
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
55 changes: 50 additions & 5 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 All @@ -50,8 +95,8 @@ func TestFprint(t *testing.T) {
pperr.Fprint(&buf, err)

actual := buf.String()
actual = regexp.MustCompile(`(?m)[^\s>]+/go/.*:\d+$`).ReplaceAllString(actual, ".../go/...:NN")
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(`
Expand Down Expand Up @@ -113,8 +158,8 @@ func TestFprint_Indent(t *testing.T) {
pperr.FprintFunc(&buf, err, pperr.NewPrinterWithIndent(">>"))

actual := buf.String()
actual = regexp.MustCompile(`(?m)[^\s>]+/go/.*:\d+$`).ReplaceAllString(actual, ".../go/...:NN")
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(`
Expand Down Expand Up @@ -154,8 +199,8 @@ func TestSprint(t *testing.T) {
err := f1(true)
actual := pperr.Sprint(err)

actual = regexp.MustCompile(`(?m)[^\s>]+/go/.*:\d+$`).ReplaceAllString(actual, ".../go/...:NN")
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(`
Expand Down Expand Up @@ -195,8 +240,8 @@ func TestSprintFunc(t *testing.T) {
err := f1(true)
actual := pperr.SprintFunc(err, pperr.NewPrinterWithIndent(">>"))

actual = regexp.MustCompile(`(?m)[^\s>]+/go/.*:\d+$`).ReplaceAllString(actual, ".../go/...:NN")
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(`
Expand Down Expand Up @@ -261,8 +306,8 @@ func TestFprint_WithoutMessage(t *testing.T) {
pperr.FprintFunc(&buf, err, pperr.PrinterWithoutMessage)

actual := buf.String()
actual = regexp.MustCompile(`(?m)[^\s>]+/go/.*:\d+$`).ReplaceAllString(actual, ".../go/...:NN")
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(`
Expand Down
2 changes: 1 addition & 1 deletion printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var DefaultPrinterWithIndent = func(w io.Writer, err error, frames, parent Frame
}
}

var PrinterWithoutMessage = func(w io.Writer, err error, frames, parent Frames) {
var PrinterWithoutMessage = func(w io.Writer, _ error, frames, parent Frames) {
if frames != nil {
if parent != nil {
frames = frames.Exclude(parent)
Expand Down