-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* `BaseStackTrace` util * refactor test
- Loading branch information
Showing
3 changed files
with
108 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package errors | ||
|
||
func BaseStackTrace(err error) []uintptr { | ||
if err == nil { | ||
return nil | ||
} | ||
return baseStackTrace(err) | ||
} | ||
|
||
func baseStackTrace(err error) []uintptr { | ||
x, ok := err.(interface{ StackTrace() []uintptr }) | ||
if !ok { | ||
return nil | ||
} | ||
stackTrace := x.StackTrace() | ||
childStackTrace := baseStackTrace(Unwrap(err)) | ||
if len(childStackTrace) == 0 { | ||
return stackTrace | ||
} | ||
return childStackTrace | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package errors_test | ||
|
||
import ( | ||
stderr "errors" | ||
"testing" | ||
|
||
"github.com/taxio/errors" | ||
) | ||
|
||
func TestBaseStackTrace(t *testing.T) { | ||
t.Run("nil", func(t *testing.T) { | ||
got := errors.BaseStackTrace(nil) | ||
if len(got) != 0 { | ||
t.Errorf("expected empty, got %v", got) | ||
} | ||
}) | ||
|
||
t.Run("other error", func(t *testing.T) { | ||
got := errors.BaseStackTrace(stderr.New("test")) | ||
if len(got) != 0 { | ||
t.Errorf("expected empty, got %v", got) | ||
} | ||
}) | ||
|
||
t.Run("single error", func(t *testing.T) { | ||
err := errors.New("test") | ||
cErr := mustCast(t, err) | ||
|
||
got := errors.BaseStackTrace(err) | ||
if len(cErr.StackTrace()) != len(got) { | ||
t.Fatalf("expected %d, got %d", len(cErr.StackTrace()), len(got)) | ||
} | ||
for i, st := range cErr.StackTrace() { | ||
if got[i] != st { | ||
t.Errorf("expected %v, got %v", st, got[i]) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("nested", func(t *testing.T) { | ||
baseErr := errors.New("base") | ||
cBaseErr := mustCast(t, baseErr) | ||
wrapErr1 := errors.Wrap(baseErr) | ||
wrapErr2 := errors.Wrap(wrapErr1) | ||
|
||
got := errors.BaseStackTrace(wrapErr2) | ||
if len(cBaseErr.StackTrace()) != len(got) { | ||
t.Fatalf("expected %d, got %d", len(cBaseErr.StackTrace()), len(got)) | ||
} | ||
for i, st := range cBaseErr.StackTrace() { | ||
if got[i] != st { | ||
t.Errorf("expected %v, got %v", st, got[i]) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("nested with other base error", func(t *testing.T) { | ||
baseErr := stderr.New("base") | ||
wrapErr1 := errors.Wrap(baseErr) | ||
cWrapErr1 := mustCast(t, wrapErr1) | ||
wrapErr2 := errors.Wrap(wrapErr1) | ||
|
||
got := errors.BaseStackTrace(wrapErr2) | ||
if len(cWrapErr1.StackTrace()) != len(got) { | ||
t.Fatalf("expected %d, got %d", len(cWrapErr1.StackTrace()), len(got)) | ||
} | ||
for i, st := range cWrapErr1.StackTrace() { | ||
if got[i] != st { | ||
t.Errorf("expected %v, got %v", st, got[i]) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
func mustCast(t *testing.T, err error) *errors.Error { | ||
t.Helper() | ||
var cErr *errors.Error | ||
if !errors.As(err, &cErr) { | ||
t.Fatalf("expected error to be *errors.Error") | ||
} | ||
return cErr | ||
} |