Skip to content

Commit

Permalink
feat(errorx): add withstack helper (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr authored Nov 9, 2020
1 parent 0ed8440 commit 9672ed2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions errorsx/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ func Cause(err error) error {
return err
}

// WithStack mirror pkg/errors.WithStack but does not wrap existing stack
// traces.
func WithStack(err error) error {
if _, ok := err.(StackTracer); ok {
return err
}

return errors.WithStack(err)
}

// StatusCodeCarrier can be implemented by an error to support setting status codes in the error itself.
type StatusCodeCarrier interface {
// StatusCode returns the status code of this error.
Expand Down
18 changes: 18 additions & 0 deletions errorsx/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package errorsx

import (
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

func TestWithStack(t *testing.T) {
t.Run("case=wrap", func(t *testing.T) {
orig := errors.New("hi")
wrap := WithStack(orig)

assert.EqualValues(t, orig.(StackTracer).StackTrace(), wrap.(StackTracer).StackTrace())
assert.EqualValues(t, orig.(StackTracer).StackTrace(), WithStack(wrap).(StackTracer).StackTrace())
})
}

0 comments on commit 9672ed2

Please sign in to comment.