From cdf856f0ab8be13e8dff0296bfa9bb2a677ce815 Mon Sep 17 00:00:00 2001 From: lysu Date: Wed, 15 May 2019 13:36:41 +0800 Subject: [PATCH] add NewNoStackError support (#13) --- juju_adaptor.go | 11 +++++++++++ stack_test.go | 10 ++++++++++ 2 files changed, 21 insertions(+) diff --git a/juju_adaptor.go b/juju_adaptor.go index 215b4b5..7abb262 100644 --- a/juju_adaptor.go +++ b/juju_adaptor.go @@ -55,6 +55,17 @@ func Annotatef(err error, format string, args ...interface{}) error { } } +var emptyStack stack + +// NewNoStackError creates error without error stack +// later duplicate trace will no longer generate Stack too. +func NewNoStackError(msg string) error { + return &fundamental{ + msg: msg, + stack: &emptyStack, + } +} + // ErrorStack will format a stack trace if it is available, otherwise it will be Error() // If the error is nil, the empty string is returned // Note that this just calls fmt.Sprintf("%+v", err) diff --git a/stack_test.go b/stack_test.go index 5603425..cf9efad 100644 --- a/stack_test.go +++ b/stack_test.go @@ -287,3 +287,13 @@ func TestNewStack(t *testing.T) { t.Errorf("NewStack(): want: %v, got: %+v", "testing.tRunner", gotFirst) } } + +func TestSuspendStackError(t *testing.T) { + err := NewNoStackError("test error") + err = Trace(err) + err = Trace(err) + result := fmt.Sprintf("%+v", err) + if result != "test error" { + t.Errorf("NewNoStackError(): want %s, got %v", "test error", result) + } +}