fix(httpcache): propagate sidecar write errors from Fetch

saveMeta now returns an error instead of silently swallowing WriteFile/
Rename failures. Fetch wraps and returns it (with updated=true, since
the body rename already succeeded). Callers get a loud signal when the
sidecar can't be written — the body is still good, but the next
conditional GET may redownload.
This commit is contained in:
AJ ONeal 2026-04-20 17:00:22 -06:00
parent f75d5c489a
commit ba64018838
No known key found for this signature in database

View File

@ -89,17 +89,21 @@ func (c *Cacher) loadMeta() {
}
// saveMeta writes etag/lastMod to the sidecar file atomically.
func (c *Cacher) saveMeta() {
func (c *Cacher) saveMeta() error {
m := cacheMeta{ETag: c.etag, LastMod: c.lastMod}
data, err := json.Marshal(m)
if err != nil {
return
return err
}
tmp := c.metaPath() + ".tmp"
if err := os.WriteFile(tmp, data, 0o644); err != nil {
return
return err
}
os.Rename(tmp, c.metaPath())
if err := os.Rename(tmp, c.metaPath()); err != nil {
_ = os.Remove(tmp)
return err
}
return nil
}
// New creates a Cacher that fetches URL and writes it to path.
@ -219,7 +223,9 @@ func (c *Cacher) Fetch() (updated bool, err error) {
if lm := resp.Header.Get("Last-Modified"); lm != "" {
c.lastMod = lm
}
c.saveMeta()
if err := c.saveMeta(); err != nil {
return true, fmt.Errorf("save meta for %s: %w", c.Path, err)
}
return true, nil
}