From c52ecbf5f68cfb98526650699d329e986186b558 Mon Sep 17 00:00:00 2001 From: Michael Bond Date: Thu, 11 May 2023 09:38:41 -0500 Subject: [PATCH 1/2] implement unwrapping of withContext errors --- errors/context_map.go | 4 ++++ errors/with_context_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/errors/context_map.go b/errors/context_map.go index 3455fa9..17d476e 100644 --- a/errors/context_map.go +++ b/errors/context_map.go @@ -77,3 +77,7 @@ func (c *withContext) FormatFields() string { } return buf.String() } + +func (c *withContext) Unwrap() error { + return c.cause +} diff --git a/errors/with_context_test.go b/errors/with_context_test.go index c31e33e..5868a1f 100644 --- a/errors/with_context_test.go +++ b/errors/with_context_test.go @@ -1,6 +1,7 @@ package errors_test import ( + "context" "fmt" "io" "strings" @@ -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 { @@ -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)) +} From cc7f46fb52b1f7b31f9f9ed29804c47aab84c15b Mon Sep 17 00:00:00 2001 From: Michael Bond Date: Fri, 12 May 2023 09:08:46 -0500 Subject: [PATCH 2/2] simplified tests --- errors/with_context_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/errors/with_context_test.go b/errors/with_context_test.go index 5868a1f..501f4c7 100644 --- a/errors/with_context_test.go +++ b/errors/with_context_test.go @@ -73,11 +73,9 @@ func TestWrapNil(t *testing.T) { } func TestUnwrap(t *testing.T) { - holsterErr := errors.WithContext{"some": "context"}.Wrap(context.Canceled, "external timeout") - holsterWrappedErr := errors.WithContext{"some": "othercontext"}.Wrap(holsterErr, "more text") + holsterWrappedErr := errors.WithContext{"some": "context"}.Wrap(context.Canceled, "external timeout") assert.True(t, stderrors.Is(holsterWrappedErr, context.Canceled)) - stdErr := fmt.Errorf("%w", context.Canceled) - stdWrappedErr := fmt.Errorf("%w more text", stdErr) + stdWrappedErr := fmt.Errorf("%w external timeout", context.Canceled) assert.True(t, stderrors.Is(stdWrappedErr, context.Canceled)) }