Skip to content

Commit

Permalink
Merge pull request #170 from mailgun/mbond/unwrap
Browse files Browse the repository at this point in the history
Implement Unwrap for withContext Errors
  • Loading branch information
mkbond authored May 12, 2023
2 parents 2f66002 + cc7f46f commit 6210080
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions errors/context_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ func (c *withContext) FormatFields() string {
}
return buf.String()
}

func (c *withContext) Unwrap() error {
return c.cause
}
11 changes: 11 additions & 0 deletions errors/with_context_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package errors_test

import (
"context"
"fmt"
"io"
"strings"
Expand All @@ -10,6 +11,8 @@ import (
"github.com/mailgun/holster/v4/callstack"
"github.com/mailgun/holster/v4/errors"
"github.com/stretchr/testify/assert"

stderrors "errors"
)

type TestError struct {
Expand Down Expand Up @@ -68,3 +71,11 @@ func TestWrapNil(t *testing.T) {
got := errors.WithContext{"some": "context"}.Wrap(nil, "no error")
assert.Nil(t, got)
}

func TestUnwrap(t *testing.T) {
holsterWrappedErr := errors.WithContext{"some": "context"}.Wrap(context.Canceled, "external timeout")
assert.True(t, stderrors.Is(holsterWrappedErr, context.Canceled))

stdWrappedErr := fmt.Errorf("%w external timeout", context.Canceled)
assert.True(t, stderrors.Is(stdWrappedErr, context.Canceled))
}

0 comments on commit 6210080

Please sign in to comment.