Skip to content

Commit

Permalink
implement unwrapping of withContext errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mkbond committed May 11, 2023
1 parent 2f66002 commit c52ecbf
Show file tree
Hide file tree
Showing 2 changed files with 17 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
}
13 changes: 13 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,13 @@ func TestWrapNil(t *testing.T) {
got := errors.WithContext{"some": "context"}.Wrap(nil, "no error")
assert.Nil(t, got)
}

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

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

0 comments on commit c52ecbf

Please sign in to comment.