From 004ee488a636a4878c7e6860b337a96f1466f58f Mon Sep 17 00:00:00 2001 From: NevS <1173325467@qq.com> Date: Thu, 23 Sep 2021 14:23:02 +0800 Subject: [PATCH] fix AtomicError panic when Set nil (#1049) (#1050) --- core/errorx/atomicerror.go | 4 +++- core/errorx/atomicerror_test.go | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/core/errorx/atomicerror.go b/core/errorx/atomicerror.go index bc52d54c..2a3db80e 100644 --- a/core/errorx/atomicerror.go +++ b/core/errorx/atomicerror.go @@ -9,7 +9,9 @@ type AtomicError struct { // Set sets the error. func (ae *AtomicError) Set(err error) { - ae.err.Store(err) + if err != nil { + ae.err.Store(err) + } } // Load returns the error. diff --git a/core/errorx/atomicerror_test.go b/core/errorx/atomicerror_test.go index d05f8866..10c7b44d 100644 --- a/core/errorx/atomicerror_test.go +++ b/core/errorx/atomicerror_test.go @@ -17,6 +17,15 @@ func TestAtomicError(t *testing.T) { assert.Equal(t, errDummy, err.Load()) } +func TestAtomicErrorSetNil(t *testing.T) { + var ( + errNil error + err AtomicError + ) + err.Set(errNil) + assert.Equal(t, errNil, err.Load()) +} + func TestAtomicErrorNil(t *testing.T) { var err AtomicError assert.Nil(t, err.Load())