Skip to content

fix: Handle non-JSON API responses gracefully - #160

Open
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix-json-parse-error
Open

fix: Handle non-JSON API responses gracefully#160
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix-json-parse-error

Conversation

@sentry

@sentry sentry Bot commented Aug 4, 2026

Copy link
Copy Markdown

This PR addresses an issue where the browser extension would crash with a SyntaxError: Unexpected token 'u', "upstream c"... is not valid JSON.

The root cause was identified as response.json() being called unconditionally on API responses, even when the response was not ok (e.g., a 502/503 gateway error) and contained a plain-text error message instead of valid JSON.

To fix this, a new private helper method safeResponseJson has been introduced in src/service.ts. This helper:

  • Checks response.ok and returns null immediately if the response is not successful.
  • Wraps the response.json() call in a try-catch block to gracefully handle parsing errors, returning null if parsing fails.

All four call sites for response.json() in fetchCommitReport, fetchPRComparison, listFlags, and listComponents have been updated to use this.safeResponseJson(response). Additionally, the return objects for these functions now correctly reflect ok: response.ok && data !== null to ensure that callers are aware of failed data retrieval, and setCached calls are guarded to prevent caching null data.

Fixes BROWSER-EXTENSION-Q5

@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Bundle Report

Changes will increase total bundle size by 216 bytes (0.03%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
codecov-browser-extension-array-push 649.3kB 216 bytes (0.03%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: codecov-browser-extension-array-push

Assets Changed:

Asset Name Size Change Total Size Change (%)
background.js 216 bytes 198.6kB 0.11%

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.

0 participants