Extract a fetchJson helper for throw-on-error fetches - #193
Merged
Conversation
The data-fetching hooks/components repeated the same "await fetch, check response.ok, otherwise throw `<label> failed (HTTP <status>)`, then parse JSON" boilerplate. Extract it into `fetchJson<T>` in utils/api.ts and use it at the five sites that share those exact semantics: - ChartView: compound_svg POST and the chart-data GET (the AbortController signal is passed straight through; AbortError still propagates). - MainDatasetView: dataset-metadata react-query queryFn. - useSearchTask: the submit_query call. - MainSelectedSet: the /reactions POST. Sites that deliberately *swallow* errors are intentionally left alone: MainReactionView, CompoundView, ReactionCard, and ModalKetcher return null/'' (or warn) on a bad response so an HTML error body never reaches dangerouslySetInnerHTML, and useSearchTask's poll branches on specific 200/202 status codes. Those don't fit a throw-on-error helper. No behavior change at the migrated sites; `npm run build` (tsc + vite) and `npm run lint` pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The Tier 2 frontend cleanup. The data-fetching hooks/components repeated the same boilerplate —
await fetch, checkresponse.ok, otherwisethrow new Error(\failed (HTTP )`), then parse JSON. This extracts it intofetchJson` in app/src/utils/api.ts and migrates the five sites that share those exact semantics:compound_svgPOST and the chart-data GET (theAbortControllersignal passes straight through —AbortErrorstill rejects before the ok-check, so the existing.catchguard is unaffected).queryFn.submit_querycall./reactionsPOST.Deliberately left alone
The other fetch sites don't share these semantics, so forcing them through the helper would change behavior or hurt readability:
MainReactionView(×2),CompoundView,ReactionCard,ModalKetcherreturnnull/''(orconsole.warn) on a bad response on purpose, so an HTML error body never reachesdangerouslySetInnerHTML. Documented inline at each site.useSearchTask's poll dispatches on specific200/202codes.MainBrowsehas nookcheck today; out of scope.Verification
npm run build(tsc typecheck + vite) passes.npm run lint(ESLint) andnpm run format:check(Prettier) clean.Follow-up
The other half of the Tier 2 frontend plan — the 4 unrendered-measurement
TODOs inConditionsView— are genuine feature gaps, so they're tracked in #192 rather than implemented here.🤖 Generated with Claude Code
Greptile Summary
This PR extracts a reusable
fetchJson<T>helper inapp/src/utils/api.tsthat centralizes the fetch-then-check-ok-then-parse-JSON pattern, and migrates five call sites that share exactly those semantics.fetchJsonhelper (api.ts): throws"${label} failed (HTTP ${status})"on non-2xx, accepts anyRequestInit(includingAbortSignal), and falls back gracefully when no label is supplied.ChartView,MainDatasetView,useSearchTask,MainSelectedSet): each replaces 5–8 lines of inline boilerplate with a singlefetchJsoncall; error-swallowing sites and the status-branching poll inuseSearchTaskare deliberately left untouched with inline explanations.Confidence Score: 5/5
Pure refactor with no behavior change at the migrated sites; safe to merge.
All five migrations preserve the original throw-on-error → parse-JSON flow. The AbortController signal passes through fetchJson unchanged, keeping the chart-data fetch's abort guard intact. The dangerouslySetInnerHTML protection in ChartView (throw on non-2xx → catch sets molHtml to null) is explicitly preserved. The one minor wording shift in MainSelectedSet's error message is console-only and not user-visible. No logic was altered.
No files require special attention.
Important Files Changed
Reviews (2): Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." | Re-trigger Greptile