Skip to content

Commit

Permalink
add SuspendStack support (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored May 15, 2019
1 parent cdf856f commit d95478c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 12 additions & 0 deletions juju_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errors

import (
"fmt"
"io"
"runtime"
"testing"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}

0 comments on commit d95478c

Please sign in to comment.