Skip to content

Refactor jsonFetch to distinguish error types (timeout vs 404 vs 5xx vs parse error) #32

Closed

Description

@neuromechanist

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)
  • AbortError from the timeout → propagates as an exception (NOT caught — caller crashes if it doesn't wrap in its own .catch)
  • SyntaxError from res.json() on a malformed body → propagates as an exception (same)
  • TypeError / network failure from fetch() 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.ts and readme.ts these calls are not wrapped in try-catch, so a JSON parse failure for summary.json surfaces as a 500 with no log line distinguishing it from a network failure."

"getLanding returning null silently fails the fast path — is that desirable, and is it logged anywhere it should be?"

"Callers have no way to tell whether null means '404 — dataset genuinely does not exist' versus '5xx/timeout — data service unavailable.' Both fall through to the manifest path and attempt getManifest, which will also likely fail (returning another silent null) and produce renderNoManifest for 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_found vs timeout)

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

  1. /api/dataset/[id]/readme.ts + /tree.ts: when getLanding is not_found, return a real 404 instead of falling through to the slow manifest path that will also fail.
  2. /dataset/[id].astro SSR: distinguish "dataset doesn't exist" (Astro 404 response) from "data layer degraded" (partial page with warning banner).
  3. All summary.json consumers: distinguish not_found (expected fallback, no warn) from timeout/parse_error/network_error (real degradation, warn).

Acceptance

  • jsonFetch returns FetchOutcome<T> — every failure mode caught + tagged
  • jsonFetchOrNull compatibility wrapper handles callers that don't need disambiguation
  • /api/dataset/[id]/* endpoints return real 404 when getLanding reports not_found
  • Every non-ok outcome (except not_found) emits a console.warn with kind + URL + reason
  • All existing tests still pass; new tests pin the outcome shapes

Notes / refs

Activity

  1. added
    enhancement
    on May 19, 2026
  2. added a commit that references this issue on May 19, 2026
    a5f762e
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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions