Skip to content

Food search: backend errors shown as 'No foods found'; stale responses overwrite results #5

Description

@KrishP147

User-reported: manual food logging search shows "no results" when it should show the closest result.

frontend/src/components/FoodSearchInput.jsx (~L437-465, ~L607): the catch swallows every error (404/5xx/network/CORS) into setResults([]); showDropdown stays true from an earlier search, so the "No foods found. Try a different search term" branch renders for failures too. No AbortController/request-id, so a slow older response can overwrite the current query's results.

Backend: supabase/functions/search-food/index.ts (~L73-77) turns a USDA 429 / DEMO_KEY rate limit into a generic 500 (falls back to DEMO_KEY when USDA_API_KEY unset, ~L21). Same in food-details/index.ts (~L17).

Fix:

  1. Add an error state; on failure show "Search unavailable, try again" (distinct from empty results). Show "No foods found" only after a 200 with empty foods.
  2. Ignore stale responses (AbortController via axios signal, or query-id ref).
  3. In search-food and food-details, map USDA 429 → 503 with detail: "Food database rate-limited, try again shortly"; log a warning when falling back to DEMO_KEY.
  4. Existing FoodSearchInput tests must pass; add tests for error state and stale-response guard.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions