| 1 |
package sturdyc |
| 2 |
|
| 3 |
import "errors" |
| 4 |
|
| 5 |
var ( |
| 6 |
// errOnlyDistributedRecords is an internal error that the cache uses to not |
| 7 |
// store records as missing if it's unable to get part of the batch from the |
| 8 |
// underlying data source. |
| 9 |
errOnlyDistributedRecords = errors.New("sturdyc: we were only able to get records from the distributed storage") |
| 10 |
// ErrNotFound should be returned from a FetchFn to indicate that a record is |
| 11 |
// missing at the underlying data source. This helps the cache to determine |
| 12 |
// if a record should be deleted or stored as a missing record if you have |
| 13 |
// that functionality enabled. Missing records are refreshed like any other |
| 14 |
// record, and if your FetchFn returns a value for it, the record will no |
| 15 |
// longer be considered missing. Please note that this only applies to |
| 16 |
// client.GetOrFetch and client.Passthrough. For client.GetOrFetchBatch and |
| 17 |
// client.PassthroughBatch, this works implicitly if you return |
| 18 |
// a map without the ID, and have store missing records enabled. |
| 19 |
ErrNotFound = errors.New("sturdyc: err not found") |
| 20 |
// ErrMissingRecord is returned by client.GetOrFetch and client.Passthrough when a record has been marked |
| 21 |
// as missing. The cache will still try to refresh the record in the background if it's being requested. |
| 22 |
ErrMissingRecord = errors.New("sturdyc: the record has been marked as missing in the cache") |
| 23 |
// ErrOnlyCachedRecords can be returned when you're using the cache with |
| 24 |
// early refreshes or distributed storage functionality. It indicates that |
| 25 |
// the records *should* have been refreshed from the underlying data source, |
| 26 |
// but the operation failed. It is up to you to decide whether you want to |
| 27 |
// proceed with the records that were retrieved from the cache. Note: For |
| 28 |
// batch operations, this might contain only part of the batch. For example, |
| 29 |
// if you requested keys 1-10, and we had IDs 1-3 in the cache, but the |
| 30 |
// request to fetch records 4-10 failed. |
| 31 |
ErrOnlyCachedRecords = errors.New("sturdyc: failed to fetch the records that were not in the cache") |
| 32 |
// ErrInvalidType is returned when you try to use one of the generic |
| 33 |
// package level functions but the type assertion fails. |
| 34 |
ErrInvalidType = errors.New("sturdyc: invalid response type") |
| 35 |
) |
| 36 |
|
| 37 |
// onlyCachedRecords is used when we were able to successfully retrieve some |
| 38 |
// records from distributed storage, but the request to get additional records |
| 39 |
// from the underlying data source failed. In this case, we wrap any potential |
| 40 |
// errors from the underlying data source with an ErrOnlyCachedRecords to allow |
| 41 |
// the user to decide whether to proceed with the cached records or not. |
| 42 |
func onlyCachedRecords(err error) error { |
| 43 |
multiErr, isMultiErr := err.(interface{ Unwrap() []error }) |
| 44 |
if !isMultiErr { |
| 45 |
if errors.Is(errOnlyDistributedRecords, err) { |
| 46 |
return ErrOnlyCachedRecords |
| 47 |
} |
| 48 |
return errors.Join(err, ErrOnlyCachedRecords) |
| 49 |
} |
| 50 |
|
| 51 |
var errs []error |
| 52 |
errs = append(errs, ErrOnlyCachedRecords) |
| 53 |
for _, e := range multiErr.Unwrap() { |
| 54 |
if !errors.Is(e, errOnlyDistributedRecords) { |
| 55 |
errs = append(errs, e) |
| 56 |
} |
| 57 |
} |
| 58 |
return errors.Join(errs...) |
| 59 |
} |