diff --git a/error_wrap.go b/error_wrap.go index e38df5c..2d8c350 100644 --- a/error_wrap.go +++ b/error_wrap.go @@ -48,11 +48,11 @@ func GetStackTrace(err error) []runtime.Frame { func UnwrapToRoot(err error) error { lastErr := err for { - e := errors.Unwrap(lastErr) - if e == nil { + errs := UnwrapMulti(lastErr) + if len(errs) == 0 { return lastErr } - lastErr = e + lastErr = errs[0] } } diff --git a/error_wrap_test.go b/error_wrap_test.go index 5760ce6..605cc1f 100644 --- a/error_wrap_test.go +++ b/error_wrap_test.go @@ -1,6 +1,7 @@ package goapperrors import ( + "errors" "testing" "github.com/stretchr/testify/assert" @@ -63,6 +64,12 @@ func Test_UnwrapToRoot(t *testing.T) { e2 := Wrap(e1) e3 := Wrap(e2) assert.Equal(t, errTest1, UnwrapToRoot(e3)) + + e10 := Wrap(errTest1) + e20 := Wrap(e10) + e21 := errors.Join(e20, errors.New("test")) + e30 := Wrap(e21) + assert.Equal(t, errTest1, UnwrapToRoot(e30)) } type err1 struct{}