From 45a87dbef1e917e9ccb9ddccb3d3dae244af305b Mon Sep 17 00:00:00 2001 From: Matt Cottam Date: Fri, 30 Aug 2024 13:56:54 +0100 Subject: [PATCH 1/2] Stop initialising the IsUnexpected flag to false for new errors --- errors_test.go | 2 +- factory.go | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/errors_test.go b/errors_test.go index 4aa9cd2..8785147 100644 --- a/errors_test.go +++ b/errors_test.go @@ -575,7 +575,7 @@ func TestSetIsRetryable(t *testing.T) { func TestSetIsUnexpected(t *testing.T) { err := New("code", "message", nil) - assert.False(t, *err.IsUnexpected) + assert.Nil(t, err.IsUnexpected) err.SetIsUnexpected(true) assert.True(t, *err.IsUnexpected) diff --git a/factory.go b/factory.go index dacd1f0..063be4b 100644 --- a/factory.go +++ b/factory.go @@ -117,10 +117,9 @@ func RateLimited(code, message string, params map[string]string) *Error { // Builds a stack based on the current call stack func errorFactory(code string, message string, params map[string]string) *Error { err := &Error{ - Code: ErrUnknown, - Message: message, - Params: map[string]string{}, - IsUnexpected: ¬Unexpected, + Code: ErrUnknown, + Message: message, + Params: map[string]string{}, } if len(code) > 0 { err.Code = code From 3c71ada78ca129192b59f9ec961808a8251a07e9 Mon Sep 17 00:00:00 2001 From: Matt Cottam Date: Fri, 30 Aug 2024 14:38:49 +0100 Subject: [PATCH 2/2] Update comment to give extra guidance --- errors.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/errors.go b/errors.go index 737daf7..1231f36 100644 --- a/errors.go +++ b/errors.go @@ -218,7 +218,8 @@ func (p *Error) Retryable() bool { } // Unexpected states whether an error is not expected to occur. In many cases this will be due to a bug, e.g. due to a -// defensive check failing +// defensive check failing. +// Note that if the IsUnexpected flag has not been set at all, this will still return false. func (p *Error) Unexpected() bool { if p.IsUnexpected != nil { return *p.IsUnexpected