Browse Source

fix AtomicError panic when Set nil (#1049) (#1050)

NevS 3 years ago
parent
commit
004ee488a6
2 changed files with 12 additions and 1 deletions
  1. 3 1
      core/errorx/atomicerror.go
  2. 9 0
      core/errorx/atomicerror_test.go

+ 3 - 1
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.

+ 9 - 0
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())