diff --git a/juju_adaptor.go b/juju_adaptor.go index 7abb262..97a4b2b 100644 --- a/juju_adaptor.go +++ b/juju_adaptor.go @@ -66,6 +66,18 @@ func NewNoStackError(msg string) error { } } +// SuspendStack suspends stack for a exists error. +// it can be used to suspend follow up Trace do not add stack. +func SuspendStack(err error) error { + if err == nil { + return err + } + return withStack{ + err, + &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 cf9efad..16e3912 100644 --- a/stack_test.go +++ b/stack_test.go @@ -2,6 +2,7 @@ package errors import ( "fmt" + "io" "runtime" "testing" ) @@ -288,7 +289,7 @@ func TestNewStack(t *testing.T) { } } -func TestSuspendStackError(t *testing.T) { +func TestNewNoStackError(t *testing.T) { err := NewNoStackError("test error") err = Trace(err) err = Trace(err) @@ -297,3 +298,13 @@ func TestSuspendStackError(t *testing.T) { t.Errorf("NewNoStackError(): want %s, got %v", "test error", result) } } + +func TestSuspendStackError(t *testing.T) { + err := io.EOF + err = SuspendStack(err) + err = Trace(err) + result := fmt.Sprintf("%+v", err) + if result != "EOF" { + t.Errorf("NewNoStackError(): want %s, got %v", "EOF", result) + } +}