Skip to content

fix: handle invalid JSON response in facets query#813

Open
cyphercodes wants to merge 1 commit intosupermemoryai:mainfrom
cyphercodes:fix/802-facets-json-parse-error
Open

fix: handle invalid JSON response in facets query#813
cyphercodes wants to merge 1 commit intosupermemoryai:mainfrom
cyphercodes:fix/802-facets-json-parse-error

Conversation

@cyphercodes
Copy link
Copy Markdown

Problem

The dashboard shows a JSON parse error on 'Total memories stored' display (Issue #802).

Error: Unexpected non-whitespace character after JSON at position 4

This happens when the facets API endpoint returns unexpected data that isn't valid JSON, causing the frontend to fail when trying to parse the response.

Solution

Add validation for the facets API response to ensure it has the expected shape before returning it. If the response is invalid, fall back to empty facets ([]) and zero total (0).

Changes

  • Added runtime validation for facets response data in memories-grid.tsx
  • Returns safe default values ({ facets: [], total: 0 }) when response is invalid
  • Prevents JSON parse errors from breaking the UI

Testing

  • Verified the fix handles malformed responses gracefully
  • UI continues to work with fallback values

Fixes #802

Add validation for facets API response to prevent JSON parse errors
when the server returns unexpected data. Falls back to empty facets
and zero total when response is invalid.

Fixes supermemoryai#802
@entelligence-ai-pr-reviews
Copy link
Copy Markdown

entelligence-ai-pr-reviews bot commented Mar 27, 2026

EntelligenceAI PR Summary

Adds defensive runtime validation for the facets API response in MemoriesGrid to guard against malformed or unexpected data shapes.

  • Validates presence and type of facets (array) field in response.data before casting
  • Validates presence and type of total (number) field in response.data before casting
  • Falls back to safe default { facets: [], total: 0 } if validation fails
  • Replaces direct unsafe cast of response.data to FacetsResponse

Confidence Score: 5/5 - Safe to Merge

Safe to merge — this PR correctly adds defensive validation in MemoriesGrid to guard against malformed facets API responses, replacing an unsafe direct cast of response.data to FacetsResponse with explicit runtime checks for the facets array and total number fields before use. The fallback to { facets: [], total: 0 } is an appropriate safe default that prevents downstream crashes from unexpected data shapes. No review comments were generated, no logic bugs or edge cases were identified, and the change is a strictly defensive improvement with no regressions introduced.

Key Findings:

  • The replacement of an unsafe response.data as FacetsResponse cast with explicit Array.isArray(response.data?.facets) and typeof response.data?.total === 'number' guards correctly prevents TypeErrors at runtime when the API returns malformed or unexpected payloads.
  • The fallback value { facets: [], total: 0 } is semantically correct for the component's use case — an empty result set is a safe no-op rather than a crash or misleading state.
  • No new issues were introduced; coverage of the single changed file was complete, and no existing unresolved comments exist in this codebase area.

@entelligence-ai-pr-reviews
Copy link
Copy Markdown

Walkthrough

This update adds runtime validation to the facets query response in the MemoriesGrid component. Instead of blindly casting response.data to FacetsResponse, the code now checks for the presence and correct types of the facets (array) and total (number) fields before casting, defaulting to { facets: [], total: 0 } when validation fails to prevent errors from malformed API responses.

Changes

File(s) Summary
apps/web/components/memories-grid.tsx Added runtime validation of the facets query response, verifying facets is an array and total is a number before casting to FacetsResponse; falls back to { facets: [], total: 0 } on validation failure.

🔗 Cross-Repository Impact Analysis

Enable automatic detection of breaking changes across your dependent repositories. → Set up now

Learn more about Cross-Repository Analysis

What It Does

  • Automatically identifies repositories that depend on this code
  • Analyzes potential breaking changes across your entire codebase
  • Provides risk assessment before merging to prevent cross-repo issues

How to Enable

  1. Visit Settings → Code Management
  2. Configure repository dependencies
  3. Future PRs will automatically include cross-repo impact analysis!

Benefits

  • 🛡️ Prevent breaking changes across repositories
  • 🔍 Catch integration issues before they reach production
  • 📊 Better visibility into your multi-repo architecture

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dashboard: JSON parse error on 'Total memories stored' display

1 participant