Repository navigation
Description
Motivation
src/lib/data-api.ts:jsonFetch<T>(url, init) currently collapses every failure mode into null:
- HTTP 404 → null (intentional)
- HTTP 429/5xx → null (logged via
console.warn, but only by status code) AbortErrorfrom the timeout → propagates as an exception (NOT caught — caller crashes if it doesn't wrap in its own.catch)SyntaxErrorfromres.json()on a malformed body → propagates as an exception (same)TypeError/ network failure fromfetch()itself → propagates as an exception (same)
Surfaced repeatedly across pr-review-toolkit reviews of PR #30 and PR #31:
"Silent failures: AbortError, SyntaxError, TypeError all propagate unhandled to callers. In
tree.tsandreadme.tsthese calls are not wrapped in try-catch, so a JSON parse failure forsummary.jsonsurfaces as a 500 with no log line distinguishing it from a network failure."
"
getLandingreturning null silently fails the fast path — is that desirable, and is it logged anywhere it should be?"
"Callers have no way to tell whether
nullmeans '404 — dataset genuinely does not exist' versus '5xx/timeout — data service unavailable.' Both fall through to the manifest path and attemptgetManifest, which will also likely fail (returning another silent null) and producerenderNoManifestfor what is actually a non-existent dataset. The user receives a confusing 'no manifest' empty state rather than a 404."
Proposal
Refactor jsonFetch to return a discriminated union instead of T | null:
export type FetchOutcome<T> =
| { kind: "ok"; value: T }
| { kind: "not_found" }
| { kind: "rate_limited" }
| { kind: "upstream_error"; status: number }
| { kind: "timeout" }
| { kind: "network_error"; message: string }
| { kind: "parse_error"; message: string };
async function jsonFetch<T>(url: string, init: DataApiInit): Promise<FetchOutcome<T>>;Every failure mode is captured AS DATA, no longer thrown. Callers can:
- Treat any non-ok outcome as "render empty state" (preserves today's behavior)
- OR branch on specific kinds (e.g., render distinct UI for
not_foundvstimeout)
Add a thin compatibility wrapper for callers that still want the T | null shape:
export async function jsonFetchOrNull<T>(url, init): Promise<T | null> {
const out = await jsonFetch<T>(url, init);
if (out.kind !== "ok") {
if (out.kind !== "not_found") console.warn(`[data-api] ${url}: ${out.kind}`, out);
return null;
}
return out.value;
}Then migrate the 4-5 callers (getLanding, getSummary, getManifest, getMetadata) where disambiguation actually changes the rendered UI.
Specific call sites that benefit
/api/dataset/[id]/readme.ts+/tree.ts: whengetLandingisnot_found, return a real 404 instead of falling through to the slow manifest path that will also fail./dataset/[id].astroSSR: distinguish "dataset doesn't exist" (Astro404response) from "data layer degraded" (partial page with warning banner).- All
summary.jsonconsumers: distinguishnot_found(expected fallback, no warn) fromtimeout/parse_error/network_error(real degradation, warn).
Acceptance
-
jsonFetchreturnsFetchOutcome<T>— every failure mode caught + tagged -
jsonFetchOrNullcompatibility wrapper handles callers that don't need disambiguation -
/api/dataset/[id]/*endpoints return real 404 whengetLandingreportsnot_found - Every non-ok outcome (except
not_found) emits aconsole.warnwith kind + URL + reason - All existing tests still pass; new tests pin the outcome shapes
Notes / refs
- Deferred from PR feat(dataset): short-circuit unpublished datasets in readme + tree endpoints #30 and PR feat(dataset): consume compact summary.json with manifest fallback #31 review threads (silent-failure-hunter findings on both)
- Companion: Clean unpublished-state render when /api/dataset/<id>/{readme,tree} has no published version #28 (clean unpublished-state) and Use compact summary.json instead of full manifest.json for dataset detail page #29 (summary.json consumer) both shipped with the current
T | nullAPI; this issue is the cleanup pass - Also touches
archiveZipUrlcomment drift flagged on PR feat(dataset): consume compact summary.json with manifest fallback #31 by comment-analyzer (pre-existing on main) — sweep along while refactoring data-api.ts
Activity
refactor(data-api): tag jsonFetch outcomes instead of collapsing to null