From 6c19282517c6010cca7e949bc37799e7ac63bfc1 Mon Sep 17 00:00:00 2001 From: Ernst Widerberg Date: Tue, 11 Mar 2025 15:41:44 +0100 Subject: [PATCH 1/2] GetOrFetch: Don't attempt unwrap if err != nil --- fetch.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fetch.go b/fetch.go index 0ffc292..66e860b 100644 --- a/fetch.go +++ b/fetch.go @@ -123,6 +123,10 @@ func (c *Client[T]) GetOrFetch(ctx context.Context, key string, fetchFn FetchFn[ // T - The type stored in the cache. func GetOrFetch[V, T any](ctx context.Context, c *Client[T], key string, fetchFn FetchFn[V]) (V, error) { res, err := getFetch[V, T](ctx, c, key, fetchFn) + if err != nil { + var zero V + return zero, err + } return unwrap[V](res, err) } From 75adb5f904c40cb8fa041173f43a3d88032f2828 Mon Sep 17 00:00:00 2001 From: Ernst Widerberg Date: Tue, 11 Mar 2025 15:52:14 +0100 Subject: [PATCH 2/2] Update GetOrFetchBatch as well --- fetch.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fetch.go b/fetch.go index 66e860b..7795531 100644 --- a/fetch.go +++ b/fetch.go @@ -231,5 +231,9 @@ func (c *Client[T]) GetOrFetchBatch(ctx context.Context, ids []string, keyFn Key func GetOrFetchBatch[V, T any](ctx context.Context, c *Client[T], ids []string, keyFn KeyFn, fetchFn BatchFetchFn[V]) (map[string]V, error) { res, err := getFetchBatch[V, T](ctx, c, ids, keyFn, fetchFn) + if err != nil { + var zero map[string]V + return zero, err + } return unwrapBatch[V](res, err) }