What you’ll build
- Dedicated 404 and 500 routes.
- A status-aware request helper.
- Not-found, retry, and scoped error-boundary states.
- An accessibility and recovery test matrix.
01
Create explicit 404 and 500 responses
Add GET /v1/orders/not-found and select 404 Not Found. Give the static JSON body a stable error key and plain-language message so the frontend can map it without inspecting prose.
Add GET /v1/orders/error with 500 Internal Server Error. Keep the response safe for display and avoid internal system details. This route tests product behavior, not a server trace.
404 Not Found:
{
"error": "order_not_found",
"message": "We could not find that order."
}
500 Internal Server Error:
{
"error": "service_unavailable",
"message": "Orders are temporarily unavailable."
}02
Parse non-success bodies without losing context
Read JSON when possible, but retain the HTTP status as the primary classification. The parser should survive unexpected bodies and provide a safe fallback message.
Return a discriminated result so components can choose a not-found layout and reserve retry treatment for service failures. Network failures should be handled separately from HTTP responses.
type Result<T> =
| { ok: true; data: T }
| { ok: false; status: number; message: string };
export async function request<T>(url: string): Promise<Result<T>> {
try {
const response = await fetch(url);
const body: unknown = await response.json().catch(() => null);
if (!response.ok) {
const message =
typeof body === "object" && body !== null &&
"message" in body && typeof body.message === "string"
? body.message
: "The request could not be completed.";
return { ok: false, status: response.status, message };
}
return { ok: true, data: body as T };
} catch {
return {
ok: false,
status: 0,
message: "Could not connect. Check your network and try again.",
};
}
}03
Render not-found and retry states in React
A 404 should explain that the requested item is absent and offer navigation back to a known place. It should not suggest repeated retries when the identifier is simply wrong.
A 500 state can preserve context and offer Retry. Disable that action while the next request runs, then replace the alert contents in place so keyboard and screen-reader users retain orientation.
// Inside your order component:
if (!result.ok && result.status === 404) {
return (
<section>
<h2>Order not found</h2>
<p>Check the order id or return to your orders.</p>
<a href="/orders">Back to orders</a>
</section>
);
}
if (!result.ok) {
return (
<section role="alert">
<h2>We couldn’t load this order</h2>
<p>{result.message}</p>
<button onClick={reload} disabled={loading}>Retry</button>
</section>
);
}04
Use an error boundary for render failures
Request state handles expected HTTP outcomes. An error boundary covers unexpected exceptions while rendering successful data—a different failure with a separate reset path.
Place the boundary around the order feature rather than the whole application. Resetting it should not destroy unrelated navigation state.
"use client";
export default function OrderError({
reset,
}: {
reset: () => void;
}) {
return (
<section role="alert">
<h2>This order view stopped unexpectedly</h2>
<p>Your other work is still available.</p>
<button onClick={reset}>Reload order view</button>
</section>
);
}A boundary does not automatically catch asynchronous event-handler failures. Handle expected fetch outcomes in request state.
05
Verify retry, empty, and focus behavior
Test the 404 path from both direct links and in-app navigation. The empty state should identify the missing object, preserve the page title, and offer a logical destination.
For 500, move focus only when useful for recovery. Ensure the alert is announced, Retry has a clear accessible name, and loading updates do not create duplicate announcements.
- 404 identifies the missing order.
- 500 does not expose internal details.
- Retry is keyboard reachable and disabled while pending.
- Alert text meets contrast requirements.
- Recovery leaves focus in a predictable location.
06
Use a compact error-state test matrix
Review a small matrix with design, engineering, and QA before release. Run each row against a stable route so copy or focus regressions are easy to reproduce.
Keep success, valid empty data, missing resources, and service failures visibly distinct.
Keep the contract predictable, then keep building.
Dedicated failure routes let frontend teams refine resilient interfaces without destabilizing a real service. Status-aware parsing, scoped boundaries, and accessibility checks make recovery intentional.
Put your next endpoint online in minutes.
Start a 14-day trial. Team workspaces are $9/month with 5 project domains, 15 routes per project, and up to 5 seats.
Start free trial →