Skip to content

Commit

Permalink
Errors now conform the pkg/errors.Cause() interface (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 authored Oct 6, 2023
1 parent 38abe5b commit 16aca5a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ func (c *fields) Is(target error) bool {
return ok
}

// Cause returns the wrapped error which was the original
// cause of the issue. We only support this because some code
// depends on github.com/pkg/errors.Cause() returning the cause
// of the error.
// deprecated use error.Is() or error.As() instead
func (c *fields) Cause() error { return c.wrapped }

func (c *fields) Error() string {
if c.msg == NoMsg {
return c.wrapped.Error()
Expand Down
6 changes: 6 additions & 0 deletions fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,9 @@ func TestFieldsStack(t *testing.T) {
assert.Equal(t, "value1", m["key1"])
assert.Equal(t, io.EOF.Error(), err.Error())
}

// Ensure errors.Fields returns an error that is compatible with `github.com/pkf/errors.Cause()`
func TestFieldsCause(t *testing.T) {
err := errors.Fields{"key1": "value1"}.Wrap(io.EOF, "message")
assert.Equal(t, io.EOF, pkgErrorCause(err))
}
7 changes: 7 additions & 0 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func (w *stack) Is(target error) bool {
return ok
}

// Cause returns the wrapped error which was the original
// cause of the issue. We only support this because some code
// depends on github.com/pkg/errors.Cause() returning the cause
// of the error.
// deprecated use error.Is() or error.As() instead
func (w *stack) Cause() error { return w.error }

func (w *stack) HasFields() map[string]any {
if child, ok := w.error.(HasFields); ok {
return child.HasFields()
Expand Down
24 changes: 24 additions & 0 deletions stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,27 @@ func TestFormatStack(t *testing.T) {
})
}
}

// Ensure errors.Stack() returns an that works with `github.com/pkf/errors.Cause()`
func TestStackCause(t *testing.T) {
err := errors.Stack(io.EOF)
assert.Equal(t, io.EOF, pkgErrorCause(err))
}

// pkgErrorCause is identical to github.com/pkg/errors.Cause()
// Much of our existing code uses that function and depends
// on our errors conforming to the old style Causer interface.
func pkgErrorCause(err error) error {
type causer interface {
Cause() error
}

for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return err
}
7 changes: 7 additions & 0 deletions wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func (e *wrappedError) Is(target error) bool {
return ok
}

// Cause returns the wrapped error which was the original
// cause of the issue. We only support this because some code
// depends on github.com/pkg/errors.Cause() returning the cause
// of the error.
// deprecated use error.Is() or error.As() instead
func (e *wrappedError) Cause() error { return e.wrapped }

func (e *wrappedError) Error() string {
if e.msg == NoMsg {
return e.wrapped.Error()
Expand Down
6 changes: 6 additions & 0 deletions wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,9 @@ func TestCause(t *testing.T) {
assert.Equal(t, "error: wrap 1: wrap 2: the cause", err.Error())
assert.Equal(t, "the cause", cause.Error())
}

// Ensure errors.wrappedError returns an error that is compatible with `github.com/pkf/errors.Cause()`
func TestWrappedCause(t *testing.T) {
err := errors.Wrap(io.EOF, "message")
assert.Equal(t, io.EOF, pkgErrorCause(err))
}

0 comments on commit 16aca5a

Please sign in to comment.