Skip to content

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

Closed

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

  1. KrishP147 commented on Sep 24, 2026

    @KrishP147
    OwnerAuthor

    Verified post-merge (d093fb2, PR #14).

    • CI: PR run 35947466034 success (Frontend Tests pass); main run 35947588843 success (Frontend Tests, Security Scan, both deploys green).
    • Diff matches handoff: AbortSignal passed to api.get + request-id stale guard; canceled requests set no error; distinct "Search unavailable, try again" panel; search-food/food-details map USDA 429 -> 503 + DEMO_KEY warning; +4 tests.
    • Deviations reconciled (decided on Krish's behalf, fit scope, no plan change):
      1. Error panel gated on error && !loading && query.length>=2 instead of showDropdown || error -> accepted; equivalent since showDropdown only true on success. Known nit: error panel ignores click-outside; acceptable (clears on next keystroke).
      2. Duplicate "Add manually" onClick -> shared handleAddManually -> accepted; pure dedupe, no behavior change.
    • Docs: troubleshooting note for 503/"Search unavailable" on branch krish/docs-food-search-503.
      Card: Done.
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