From 8aa14098e2f1910ff7787221460aa53f7747c1f1 Mon Sep 17 00:00:00 2001 From: wbflooksky <52812629+wbflooksky@users.noreply.github.com> Date: Wed, 19 May 2021 17:37:19 +0800 Subject: [PATCH 1/6] feat: --- errors.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/errors.go b/errors.go index 161aea25..13887572 100644 --- a/errors.go +++ b/errors.go @@ -67,10 +67,9 @@ // // New, Errorf, Wrap, and Wrapf record a stack trace at the point they are // invoked. This information can be retrieved with the following interface: -// -// type stackTracer interface { -// StackTrace() errors.StackTrace -// } +type stackTracer interface { + StackTrace() errors.StackTrace +} // // The returned errors.StackTrace type is defined as // @@ -106,6 +105,23 @@ func New(message string) error { } } +// ExistStack return an error already with stack +// ExistStack will check all parent error +func ExistStack(err error) bool { + type stackTracer interface { + StackTrace() StackTrace + } + if _, ok := err.(stackTracer); ok { + return true + } + type causer interface { + Cause() error + } + if _, ok := err.(causer); !ok { + return false + } + return ExistStack(err.Cause()) +} // Errorf formats according to a format specifier and returns the string // as a value that satisfies error. // Errorf also records the stack trace at the point it was called. From b3e14fafbaa846b3accea2fd58f36f80d65f4ffb Mon Sep 17 00:00:00 2001 From: wbflooksky <52812629+wbflooksky@users.noreply.github.com> Date: Wed, 19 May 2021 17:57:27 +0800 Subject: [PATCH 2/6] feat func ExistStack(err error) bool: non-intrusive: Add a public method to determine if the error has been appended to the stack. Avoid adding the stack repeatedly for errors. --- errors.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/errors.go b/errors.go index 13887572..d2a53998 100644 --- a/errors.go +++ b/errors.go @@ -67,9 +67,10 @@ // // New, Errorf, Wrap, and Wrapf record a stack trace at the point they are // invoked. This information can be retrieved with the following interface: -type stackTracer interface { - StackTrace() errors.StackTrace -} +// +// type stackTracer interface { +// StackTrace() errors.StackTrace +// } // // The returned errors.StackTrace type is defined as // From 5969e207acfa8ce13b87c2333185580af9e34730 Mon Sep 17 00:00:00 2001 From: wbflooksky <52812629+wbflooksky@users.noreply.github.com> Date: Wed, 19 May 2021 18:22:34 +0800 Subject: [PATCH 3/6] fix: error method --- errors.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/errors.go b/errors.go index d2a53998..e1bf4dc3 100644 --- a/errors.go +++ b/errors.go @@ -118,10 +118,11 @@ func ExistStack(err error) bool { type causer interface { Cause() error } - if _, ok := err.(causer); !ok { + if value, ok := err.(causer); ok { + return ExistStack(err.Cause()) + } else { return false } - return ExistStack(err.Cause()) } // Errorf formats according to a format specifier and returns the string // as a value that satisfies error. From 88c8636aea339f9c77a2eae2ee8cb951038616f1 Mon Sep 17 00:00:00 2001 From: wbflooksky <52812629+wbflooksky@users.noreply.github.com> Date: Wed, 19 May 2021 18:24:48 +0800 Subject: [PATCH 4/6] feat: test ExistStack add ExistStack test --- errors_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/errors_test.go b/errors_test.go index 2089b2f7..6b0b6db9 100644 --- a/errors_test.go +++ b/errors_test.go @@ -27,6 +27,23 @@ func TestNew(t *testing.T) { } } +func TestExistStack(t *testing.T) { + tests := []struct { + err error + want bool + }{ + {io.EOF, false}, + {Wrap(io.EOF, "read error"), true}, + } + + for _, tt := range tests { + got := ExistStack(tt.err) + if got != tt.want { + t.Errorf("ExistStack(%v): got: %v, want %v", tt.err, got, tt.want) + } + } +} + func TestWrapNil(t *testing.T) { got := Wrap(nil, "no error") if got != nil { From 0247cb8ef2bcd9f1bd901c642de4dc6b9ef2eb70 Mon Sep 17 00:00:00 2001 From: wbflooksky <52812629+wbflooksky@users.noreply.github.com> Date: Wed, 19 May 2021 18:56:34 +0800 Subject: [PATCH 5/6] fix: error variable name --- errors.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/errors.go b/errors.go index e1bf4dc3..4a828209 100644 --- a/errors.go +++ b/errors.go @@ -119,7 +119,7 @@ func ExistStack(err error) bool { Cause() error } if value, ok := err.(causer); ok { - return ExistStack(err.Cause()) + return ExistStack(value.Cause()) } else { return false } From 5c191f96b9c4670a8c0332d4effe70f5ffcfb1bb Mon Sep 17 00:00:00 2001 From: wbflooksky <52812629+wbflooksky@users.noreply.github.com> Date: Wed, 19 May 2021 19:01:18 +0800 Subject: [PATCH 6/6] style: optimizing code structure --- errors.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/errors.go b/errors.go index 4a828209..84bade4b 100644 --- a/errors.go +++ b/errors.go @@ -123,6 +123,11 @@ func ExistStack(err error) bool { } else { return false } + cause, ok := err.(causer) + if !ok { + return false + } + return ExistStack(cause.Cause()) } // Errorf formats according to a format specifier and returns the string // as a value that satisfies error.