Every team writes a happy path. Most teams forget about the error paths until something breaks in production. This recipe shows how to exercise every error your client should handle by forcing status codes from the server, so you can verify the user sees something sensible no matter what comes back.
The trick
GoRest v3 has a tool for this: GET /public/v3/http/status/<code>. It answers with the status code you ask for, from 200 to 599. For 4xx and 5xx the body is Problem Details (application/problem+json) with code set to requested_status. The response also carries the headers a real one has: Retry-After on 429 and 503, and WWW-Authenticate on 401. No token is needed.
curl -i "https://gorest.co.in/public/v3/http/status/401"
curl -i "https://gorest.co.in/public/v3/http/status/422"
curl -i "https://gorest.co.in/public/v3/http/status/429"
curl -i "https://gorest.co.in/public/v3/http/status/500"The body looks like this:
{
"type": "https://www.gorest.co.in/docs/api/errors#requested_status",
"title": "Too Many Requests",
"status": 429,
"code": "requested_status",
"detail": "This is the status you asked for.",
"instance": "/public/v3/http/status/429",
"request_id": "01a1933c-ee77-7d46-85b7-9f22a7f8c624"
}The forced 422 has no errors list, because no field was checked. A real 422 from POST /public/v3/users has one entry per bad field, each with a pointer such as /email. You can get a real one with a token: post {"name": "Test"} without an email. For unit tests, use a canned body in that shape.
A handler that branches on every case
The pattern: branch on status before the body. 401 is its own case (sign in again). 422 is its own case (show field errors). 429 needs a retry. 4xx without a special case is "something looks wrong with the request". 5xx is "the server failed". Network errors are a separate case too.
async function fetchUsers(url = "https://gorest.co.in/public/v3/users") {
try {
const res = await fetch(url, {
headers: { Authorization: `Bearer ${TOKEN}` }
});
if (res.status === 401) {
return showSignIn();
}
if (res.status === 422) {
const problem = await res.json();
// problem.errors = [{ pointer: "/email", code: "taken", detail: "has already been taken" }]
return showFieldErrors(problem.errors ?? []);
}
if (res.status === 429) {
const wait = Number(res.headers.get("retry-after")) || 1;
return scheduleRetry(wait);
}
if (!res.ok) {
const problem = await res.json();
throw new Error(problem.detail || `Got ${res.status}`);
}
const { data } = await res.json();
return data;
} catch (err) {
showServerError(err.message);
}
}Retry-After is in seconds. Every v3 error also has a request_id. Show it in your "something went wrong" message or log it, so a support request can point to the exact call. The code field (for example unauthorized or validation_failed) is stable, so branch on code when you need more detail than the status.
Test it
Three integration tests, one per error type. Pass the URL of the status tool so the test never depends on real failure conditions:
// tests/error-handler.test.js
import { test, expect, vi } from "vitest";
const status = (code) => `https://gorest.co.in/public/v3/http/status/${code}`;
test("401 redirects to sign-in", async () => {
await fetchUsers(status(401));
expect(window.location.pathname).toBe("/sign-in");
});
test("422 shows field errors", async () => {
// The tool has no field list, so mock this one answer.
vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(new Response(
JSON.stringify({ status: 422, code: "validation_failed",
errors: [{ pointer: "/email", code: "blank", detail: "can't be blank" }] }),
{ status: 422, headers: { "content-type": "application/problem+json" } }
));
await fetchUsers();
expect(screen.getByRole("alert")).toHaveTextContent(/can't be blank/i);
});
test("429 schedules retry", async () => {
const timer = vi.spyOn(globalThis, "setTimeout");
await fetchUsers(status(429));
expect(timer).toHaveBeenCalled();
});These tests are fast (no real failures, no waiting), deterministic (forced status codes do not vary), and cheap. Note that every call still counts against your limit: 120 calls a minute per IP without a token. A forced 429 does not use up your real budget, but a loop of tests can.
Map status codes to user-facing messages
Showing "Got 422" in the UI is a sign that you have not thought about errors. Map every code to a sentence the user can understand and act on:
const ERROR_MESSAGES = {
400: "Something looks wrong with the request. Please refresh.",
401: "Your session expired. Please sign in again.",
403: "You do not have permission to do that.",
404: "We could not find what you were looking for.",
412: "This was changed somewhere else. Reload and try again.",
422: "Some fields need attention.",
429: "Slow down a moment. We are throttling requests.",
500: "Something broke on our side. Please try again.",
503: "We are briefly unavailable. Try in a moment."
};
function userFacingError(status) {
return ERROR_MESSAGES[status] || "Something went wrong.";
}The 401 message tells the user to sign in. The 422 message tells them to look at the form. The 429 message tells them to wait. Each one says what to do.
Build a "kitchen sink" page for QA
A small page in your app that lets QA force every error in turn is very useful. Buttons that hit /http/status/<code> for each code, plus a third for "delay 5s" (/http/delay/5000), plus a fourth for "no network" (wrap fetch in a Promise that rejects). One review, every error path covered.
Common mistakes
- Catching too broadly. A blanket
try/catcharound the fetch turns 401 into "something went wrong", and the user gets a useless message. Branch on status first. - Eating the body. On 422, the body holds the field errors in
errors[]; read it before showing a generic message. - Retrying on 4xx. 4xx errors are caused by the request, so retrying gives you the same response. Only retry on 429, 503, and 5xx.
- Showing stack traces. Errors in production should not leak internal details. Use the message map; log the technical error and the
request_idsomewhere only developers see.
Combine with simulated latency
v3 has no delay parameter on other endpoints. The delay has a path of its own, GET /public/v3/http/delay/<ms> (0 to 10000 ms). To test "slow, then 500", call the delay first and then the status tool:
await fetch("https://gorest.co.in/public/v3/http/delay/3000");
const res = await fetch("https://gorest.co.in/public/v3/http/status/500");This is the worst case for many UIs: a long wait, then an error. Use it to verify your skeleton to error transition does not flash, race, or leave the page in an inconsistent state.
Keep building
Test loading skeletons with simulated latency
Use /http/delay/<ms> to make GoRest take seconds to respond, so you can verify your skeleton screens render without flicker.
Build a paginated user list in React
A working component that fetches a page, renders rows, and uses the v3 cursor to render Previous/Next correctly.
Build infinite scroll with IntersectionObserver
Append pages as the sentinel element scrolls into view. Works with any list endpoint that returns a next cursor.