ai cooking
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

not found doesn't count as an error (#464)

* not found doesn't count as an error

* prefer slog of log.fatal

* limit and lint

authored by

Paul Miller and committed by
GitHub
82eb279e 6f5dbb42

+22 -10
+14 -5
cmd/wholefoods/main.go
··· 8 8 "log" 9 9 "log/slog" 10 10 "net/http" 11 + "os" 11 12 "time" 12 13 13 14 "careme/internal/cache" ··· 37 38 38 39 cacheStore, err := cache.EnsureCache(wholefoods.Container) 39 40 if err != nil { 40 - log.Fatalf("failed to create cache: %v", err) 41 + slog.ErrorContext(ctx, "failed to create cache", "error", err) 42 + os.Exit(1) 41 43 } 42 44 43 45 httpClient := &http.Client{Timeout: time.Duration(timeoutSec) * time.Second} ··· 45 47 46 48 refs, err := resolveStoreReferences(ctx, cacheStore, httpClient, sitemapURL) 47 49 if err != nil { 48 - log.Fatalf("failed to resolve store references: %v", err) 50 + slog.ErrorContext(ctx, "failed to resolve store references", "error", err) 51 + os.Exit(1) 49 52 } 50 53 if len(refs) == 0 { 51 - log.Fatalf("no Whole Foods store references found") 54 + slog.ErrorContext(ctx, "no Whole Foods store references found", "error", err) 55 + os.Exit(1) 52 56 } 53 57 54 58 slog.Info("syncing Whole Foods store summaries", "count", len(refs)) ··· 56 60 for _, ref := range refs { 57 61 summary, err := client.StoreSummary(ctx, ref.ID) 58 62 if err != nil { 59 - slog.Warn("failed to fetch Whole Foods store summary", "store_id", ref.ID, "url", ref.URL, "error", err) 63 + if !errors.Is(err, wholefoods.ErrNotFound) { 64 + slog.ErrorContext(ctx, "failed to fetch Whole Foods store summary", "store_id", ref.ID, "url", ref.URL, "error", err) 65 + } else { 66 + slog.InfoContext(ctx, err.Error(), "store_id", ref.ID, "url", ref.URL) 67 + } 60 68 continue 61 69 } 62 70 if err := wholefoods.CacheStoreSummary(ctx, cacheStore, summary); err != nil { ··· 68 76 } 69 77 70 78 if err := wholefoods.RebuildLocationIndex(ctx, cacheStore, locations.LoadCentroids()); err != nil { 71 - log.Fatalf("failed to rebuild Whole Foods location index: %v", err) 79 + slog.ErrorContext(ctx, "failed to rebuild Whole Foods location index", "error", err) 80 + os.Exit(1) 72 81 } 73 82 74 83 fmt.Printf("synced %d Whole Foods store summaries\n", synced)
+8 -5
internal/wholefoods/client.go
··· 193 193 } 194 194 } 195 195 196 + var ErrNotFound = fmt.Errorf("store not found") 197 + 196 198 // StoreSummary fetches a store summary payload like /api/stores/10216/summary. 197 199 func (c *Client) StoreSummary(ctx context.Context, store string) (*StoreSummaryResponse, error) { 198 200 store = strings.TrimSpace(store) ··· 224 226 _ = resp.Body.Close() 225 227 }() 226 228 227 - body, err := io.ReadAll(io.LimitReader(resp.Body, 2*1024*1024)) 228 - if err != nil { 229 - return fmt.Errorf("read response: %w", err) 229 + if resp.StatusCode == http.StatusNotFound { 230 + return fmt.Errorf("%w: %s", ErrNotFound, endpoint) 230 231 } 232 + 231 233 if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { 232 - return fmt.Errorf("request failed: status %d: %s", resp.StatusCode, strings.TrimSpace(string(body))) 234 + buf, _ := io.ReadAll(io.LimitReader(resp.Body, 4000)) 235 + return fmt.Errorf("request failed: status %d: %s", resp.StatusCode, buf) 233 236 } 234 237 235 - if err := json.Unmarshal(body, dest); err != nil { 238 + if err := json.NewDecoder(resp.Body).Decode(dest); err != nil { 236 239 return fmt.Errorf("decode response: %w", err) 237 240 } 238 241 return nil