Skip to content

Commit

Permalink
fix: Check err before assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
viccon committed May 7, 2024
1 parent d8586a5 commit c9937b2
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ import (

func fetchAndCache[V, T any](ctx context.Context, c *Client[T], key string, fetchFn FetchFn[V]) (V, error) {
response, err := fetchFn(ctx)
res, ok := any(response).(T)
if !ok {
return response, errors.New("invalid response type")
}

if err != nil && c.storeMisses && errors.Is(err, ErrStoreMissingRecord) {
c.SetMissing(key, res, true)
var zero T
c.SetMissing(key, zero, true)
return response, ErrMissingRecord
}

if err != nil {
return response, err
}

res, ok := any(response).(T)
if !ok {
return response, ErrInvalidType
}

c.SetMissing(key, res, false)
return response, nil
}
Expand Down

0 comments on commit c9937b2

Please sign in to comment.