Data fetching failure
What you need to know
A successful empty result and a failed data request are different outcomes. A catalog with no books should show an empty state; a catalog whose source is unavailable should communicate failure. Handle expected outcomes explicitly and let unexpected failures reach the appropriate error boundary.
By the end, you can: handle expected errors; distinguish empty and unavailable data.
Validate a bounded synthetic upstream response
type Result = { kind: "ok"; titles: string[] } | { kind: "unavailable" };export function decodeCatalog(status: number, body: unknown): Result { if (status !== 200 || !Array.isArray(body) || body.length > 100 || !body.every(x => typeof x === "string" && x.length <= 120)) { return { kind: "unavailable" }; } return { kind: "ok", titles: body };}Expected behavior (not framework-tested): 200 with [] is a successful empty catalog; 503 with [] or 200 with a malformed object is unavailable. This helper models response classification, not network execution.
How it works
fetch resolves to a Response even for many HTTP error statuses, whereas transport errors can reject the promise. A JSON parser can also throw. Classify expected failures from the known dependency contract; do not convert every programming defect into an empty array. A missing individual book is a different case from a failed database read. For an actual HTTP adapter, bound response bytes and request duration before accepting a complete body, validate the parsed shape, and avoid sending upstream error details to the browser. The array length check in this helper happens after parsing and cannot prevent an oversized body from consuming memory. An adapter must enforce its own byte limit during the read, not trust Content-Length alone. In a streamed route, a visible fallback or error view does not by itself establish the HTTP status.
Render distinct states (partial app/catalog/page.tsx)
import { loadCatalog } from "../../lib/mock-upstream";export default async function Catalog() { const result = await loadCatalog("empty"); if (result.kind === "unavailable") return <p role="status">Catalog temporarily unavailable.</p>; if (result.titles.length === 0) return <p>No books yet.</p>; return <ul>{result.titles.map(title => <li key={title}>{title}</li>)}</ul>;}Expected behavior (not framework-tested): The fixture supplies distinct unique titles. The empty scenario displays No books yet; the unavailable scenario displays a different message.
Common mistake
Do not use catch { return [] } as a blanket repair. It makes outages look like legitimate zero results and can cause an incorrect static build. Avoid printing internal exception messages or response bodies to users.
Practice and verification
Build a table with 200/list, 200/empty, 503, malformed body and rejected transport. Decide which outcomes are expected and which should trigger diagnostics. Verify the pure decoder cases in the staged tests; real HTTP rejection and UI behavior require a separate app test.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.