Skip to content

Commit 3f3df7c

Browse files
committed
fix: improve error handling in ChainCache.Set method
1 parent 2c3a5f4 commit 3f3df7c

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

lib/cache/chain.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,10 @@ func (c *ChainCache[T]) Set(ctx context.Context, key any, object T, options ...s
7878
err := cache.Set(ctx, key, object, options...)
7979
if err != nil {
8080
storeType := cache.GetCodec().GetStore().GetType()
81-
errs = append(errs, fmt.Errorf("Unable to set item into cache with store '%s': %v", storeType, err))
81+
errs = append(errs, fmt.Errorf("unable to set item into cache with store '%s': %w", storeType, err))
8282
}
8383
}
84-
if len(errs) > 0 {
85-
errStr := ""
86-
for k, v := range errs {
87-
errStr += fmt.Sprintf("error %d of %d: %v", k+1, len(errs), v.Error())
88-
}
89-
return errors.New(errStr)
90-
}
91-
92-
return nil
84+
return errors.Join(errs...)
9385
}
9486

9587
// Delete removes a value from all available caches

lib/cache/chain_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cache
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"testing"
87
"time"
98

@@ -246,7 +245,8 @@ func TestChainSetWhenErrorOnSetting(t *testing.T) {
246245

247246
// Then
248247
assert.Error(t, err)
249-
assert.Equal(t, fmt.Sprintf("error 1 of 1: Unable to set item into cache with store 'store1': %s", expectedErr.Error()), err.Error())
248+
assert.ErrorIs(t, err, expectedErr)
249+
assert.ErrorContains(t, err, "unable to set item into cache with store 'store1'")
250250
}
251251

252252
func TestChainDelete(t *testing.T) {
@@ -447,7 +447,8 @@ func TestChainSetWhenErrorInChain(t *testing.T) {
447447
// When - Then
448448
err := cache.Set(ctx, key, value, nil)
449449

450-
expErr := errors.New("error 1 of 1: Unable to set item into cache with store 'store1': an issue occurred with the cache")
451450
// Then
452-
assert.Equal(t, expErr, err)
451+
assert.Error(t, err)
452+
assert.ErrorIs(t, err, interError)
453+
assert.ErrorContains(t, err, "unable to set item into cache with store 'store1'")
453454
}

0 commit comments

Comments
 (0)