Skip to content

Commit

Permalink
fix: Improved the error messages and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecreature committed May 8, 2024
1 parent af46741 commit d776a7c
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@ package sturdyc
import "errors"

var (
// ErrStoreMissingRecord should be returned from FetchFn to indicate that we
// want to store the record with a cooldown. This only applies to the FetchFn,
// for the BatchFetchFn you should enable the functionality through options,
// and simply return a map without the record being present.
ErrStoreMissingRecord = errors.New("record not found")
// ErrMissingRecord is returned by client.GetFetch when
// a record has been stored as a missing record.
ErrMissingRecord = errors.New("record is missing")
// ErrOnlyCachedRecords is returned by sturdyc.GetFetchBatch when we have
// some of the requested records in the cache, but the call to fetch the
// remaining records failed. The consumer can then choose if they want to
// proceed with the cached records or retry the operation.
ErrOnlyCachedRecords = errors.New("failed to fetch the records that we did not have cached")
// ErrStoreMissingRecord should be returned from a FetchFn to indicate that a record is
// to be marked as missing by the cache. This will prevent continuous outgoing requests
// to the source. The record will still be refreshed like any other record, and if your
// FetchFn returns a value for it, the record will no longer be considered missing.
// Please note that this only applies to client.GetFetch and client.Passthrough. For
// client.GetFetchBatch and client.PassthroughBatch, this works implicitly if you return
// a map without the ID, and have store missing records enabled.
ErrStoreMissingRecord = errors.New("the record will be marked as missing in the cache")
// ErrMissingRecord is returned by client.GetFetch and client.Passthrough when a record has been marked
// as missing. The cache will still try to refresh the record in the background if it's being requested.
ErrMissingRecord = errors.New("the record has been marked as missing in the cache")
// ErrOnlyCachedRecords is returned by client.GetFetchBatch and client.PassthroughBatch
// when some of the requested records are available in the cache, but the attempt to
// fetch the remaining records failed. As the consumer, you can then decide whether to
// proceed with the cached records or if the entire batch is necessary.
ErrOnlyCachedRecords = errors.New("failed to fetch the records that were not in the cache")
// ErrInvalidType is returned when you try to use one of the generic
// package level functions, and the type assertion fails.
// package level functions but the type assertion fails.
ErrInvalidType = errors.New("invalid response type")
// ErrDeletedRecord should be returned by the function that is passed to
// client.GetFetch if you wan't to have a record deleted by a refresh.
ErrDeleteRecord = errors.New("record has been deleted at source")
// ErrDeletedRecord should be returned by the FetchFn that is passed to client.GetFetch
// and client.Passthrough if you wan't to have a record deleted by a refresh. This is
// not needed for client.GetFetchBatch and client.PassthroughBatch as the cache will
// delete the record automatically if stampede protection is enabled and store missing
// records is set to false.
ErrDeleteRecord = errors.New("the record has been deleted at the data source")
)

func ErrIsStoreMissingRecordOrMissingRecord(err error) bool {
if err == nil {
return false
}
return errors.Is(err, ErrStoreMissingRecord) || errors.Is(err, ErrMissingRecord)
}

0 comments on commit d776a7c

Please sign in to comment.