External callers depend on more than a successful JSON body. Error codes and their HTTP mapping form a public contract that should survive wording changes and internal refactors.
Read, predict, run
The model produces a minimal status and code pair. Internal details are deliberately absent. The route handler should attach a request id for support while logging diagnostic details only on the server:
JavaScript
export function run(code) { const status = new Map( Object.entries({ invalid: 400, unauthenticated: 401, forbidden: 403, missing: 404, conflict: 409, }), ).get(code) ?? 500; return { status, code: status === 500 ? "internal_error" : code };}
Before pressing Run, predict what run returns for "forbidden" and "SQL_ERROR", from the
code above. Write your answers down first: after Run, the case table shows the
expected values beside the actual ones.
Version a Service’s Error VocabularyNext.js · 7 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Review Queue · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import Cases from "./Cases.jsx";
import OldClient from "./OldClient.jsx";
export default function Page() {
return (
<main>
<h1>Version a Service’s Error Vocabulary</h1>
<p>
lib/contract.js maps internal codes to public ones. lib/client.js is a
client written before your change.
</p>
<OldClient />
<Cases />
</main>
);
}
import { run, examples } from "@/lib/contract.js";
const text = (value) => JSON.stringify(value) ?? "undefined";
export default async function Cases() {
const results = await Promise.all(
examples.map(async ([input, expected]) => {
// Record the input before running, to show whether run changed it.
const before = text(input);
const actual = await run(input);
return {
input: before,
expected,
actual,
kept: text(input) === before,
pass: text(actual) === text(expected),
};
}),
);
return (
<section>
<h2>Executable cases</h2>
<p>
Checks: {results.filter((row) => row.pass).length}/{results.length}{" "}
passed
</p>
<table>
<thead>
<tr>
<th>Input</th>
<th>Expected</th>
<th>Actual</th>
<th>Input kept</th>
</tr>
</thead>
<tbody>
{results.map((row, i) => (
<tr key={i}>
<td>
<code>{row.input}</code>
</td>
<td>
<code>{text(row.expected)}</code>
</td>
<td className={row.pass ? "pass" : "fail"}>
<code>{text(row.actual)}</code>
</td>
<td className={row.kept ? "pass" : "fail"}>
{row.kept ? "yes" : "no: run changed it"}
</td>
</tr>
))}
</tbody>
</table>
</section>
);
}
import { run } from "@/lib/contract.js";
import { messageFor } from "@/lib/client.js";
const codes = ["forbidden", "conflict", "precondition_failed", "SQL_ERROR"];
export default function OldClient() {
return (
<table aria-label="What an older client shows">
<thead>
<tr>
<th>Server code</th>
<th>Response</th>
<th>Older client shows</th>
</tr>
</thead>
<tbody>
{codes.map((code) => {
const response = run(code);
return (
<tr key={code}>
<td>{code}</td>
<td>
{response.status} {response.code}
</td>
<td>{messageFor(response)}</td>
</tr>
);
})}
</tbody>
</table>
);
}
body {
font: 16px/1.5 system-ui;
margin: 0;
color: #182b3c;
background: #f4f8fc;
}
header,
main {
max-width: 900px;
margin: auto;
padding: 20px;
}
header {
border-bottom: 1px solid #cbd5e1;
}
a {
color: #075985;
}
button,
input,
textarea,
select {
font: inherit;
padding: 8px;
margin: 4px;
}
button {
cursor: pointer;
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 10px;
border: 1px solid #cbd5e1;
text-align: left;
vertical-align: top;
}
pre,
code {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.pass {
color: #166534;
}
.fail,
[role="alert"] {
color: #a01818;
}
section {
margin: 16px 0;
padding: 12px;
background: white;
border-radius: 8px;
}
nav {
display: flex;
gap: 16px;
}
// A client released before precondition_failed existed. It knows some codes
// by name; for anything else it falls back on the status class.
export function messageFor({ status, code }) {
if (code === "conflict") return "Someone else changed this. Reload and try again.";
if (code === "forbidden") return "You can't do that.";
if (status >= 400 && status < 500) return "This request can't be completed as sent.";
return "Something went wrong on our side. Try again later.";
}
export function run(code) {
const status =
new Map(
Object.entries({
invalid: 400,
unauthenticated: 401,
forbidden: 403,
missing: 404,
conflict: 409,
}),
).get(code) ?? 500;
return { status, code: status === 500 ? "internal_error" : code };
}
export const examples = [
["forbidden", { status: 403, code: "forbidden" }],
["SQL_ERROR", { status: 500, code: "internal_error" }],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
The case table computes these results on the server side of the simulation.
It compares values; it does not prove a production deployment or database works.
Changing the vocabulary without breaking callers
Once a code is published, clients branch on it. Some changes keep every
existing client working; others don't:
Change
Safe for existing clients?
Add a new code, with a status that fits it
Yes, if clients handle unknown codes
Add an optional field, such as requestId
Yes
Rename a code (missing to not_found)
No: clients checking missing stop matching
Give an existing code a new status
No: retry and monitoring rules change
Remove a code
No
The first row has a condition, and it's the client's half of the contract:
a client must treat a code it doesn't know by its status class. An unknown
4xx means "don't retry this as it is"; an unknown 5xx means "our side failed".
lib/client.js in the lab is a client written before the exercise below:
JavaScript
export function messageFor({ status, code }) { if (code === "conflict") return "Someone else changed this. Reload and try again."; if (code === "forbidden") return "You can't do that."; if (status >= 400 && status < 500) return "This request can't be completed as sent."; return "Something went wrong on our side. Try again later.";}
Run the lab and read the Older client shows column. Then predict how the
precondition_failed row changes once the server knows that code.
An older client meets the vocabularyNext.js · 7 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Review Queue · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import Cases from "./Cases.jsx";
import OldClient from "./OldClient.jsx";
export default function Page() {
return (
<main>
<h1>Version a Service’s Error Vocabulary</h1>
<p>
lib/contract.js maps internal codes to public ones. lib/client.js is a
client written before your change.
</p>
<OldClient />
<Cases />
</main>
);
}
import { run, examples } from "@/lib/contract.js";
const text = (value) => JSON.stringify(value) ?? "undefined";
export default async function Cases() {
const results = await Promise.all(
examples.map(async ([input, expected]) => {
// Record the input before running, to show whether run changed it.
const before = text(input);
const actual = await run(input);
return {
input: before,
expected,
actual,
kept: text(input) === before,
pass: text(actual) === text(expected),
};
}),
);
return (
<section>
<h2>Executable cases</h2>
<p>
Checks: {results.filter((row) => row.pass).length}/{results.length}{" "}
passed
</p>
<table>
<thead>
<tr>
<th>Input</th>
<th>Expected</th>
<th>Actual</th>
<th>Input kept</th>
</tr>
</thead>
<tbody>
{results.map((row, i) => (
<tr key={i}>
<td>
<code>{row.input}</code>
</td>
<td>
<code>{text(row.expected)}</code>
</td>
<td className={row.pass ? "pass" : "fail"}>
<code>{text(row.actual)}</code>
</td>
<td className={row.kept ? "pass" : "fail"}>
{row.kept ? "yes" : "no: run changed it"}
</td>
</tr>
))}
</tbody>
</table>
</section>
);
}
import { run } from "@/lib/contract.js";
import { messageFor } from "@/lib/client.js";
const codes = ["forbidden", "conflict", "precondition_failed", "SQL_ERROR"];
export default function OldClient() {
return (
<table aria-label="What an older client shows">
<thead>
<tr>
<th>Server code</th>
<th>Response</th>
<th>Older client shows</th>
</tr>
</thead>
<tbody>
{codes.map((code) => {
const response = run(code);
return (
<tr key={code}>
<td>{code}</td>
<td>
{response.status} {response.code}
</td>
<td>{messageFor(response)}</td>
</tr>
);
})}
</tbody>
</table>
);
}
body {
font: 16px/1.5 system-ui;
margin: 0;
color: #182b3c;
background: #f4f8fc;
}
header,
main {
max-width: 900px;
margin: auto;
padding: 20px;
}
header {
border-bottom: 1px solid #cbd5e1;
}
a {
color: #075985;
}
button,
input,
textarea,
select {
font: inherit;
padding: 8px;
margin: 4px;
}
button {
cursor: pointer;
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 10px;
border: 1px solid #cbd5e1;
text-align: left;
vertical-align: top;
}
pre,
code {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.pass {
color: #166534;
}
.fail,
[role="alert"] {
color: #a01818;
}
section {
margin: 16px 0;
padding: 12px;
background: white;
border-radius: 8px;
}
nav {
display: flex;
gap: 16px;
}
// A client released before precondition_failed existed. It knows some codes
// by name; for anything else it falls back on the status class.
export function messageFor({ status, code }) {
if (code === "conflict") return "Someone else changed this. Reload and try again.";
if (code === "forbidden") return "You can't do that.";
if (status >= 400 && status < 500) return "This request can't be completed as sent.";
return "Something went wrong on our side. Try again later.";
}
export function run(code) {
const status =
new Map(
Object.entries({
invalid: 400,
unauthenticated: 401,
forbidden: 403,
missing: 404,
conflict: 409,
}),
).get(code) ?? 500;
return { status, code: status === 500 ? "internal_error" : code };
}
export const examples = [
["forbidden", { status: 403, code: "forbidden" }],
["SQL_ERROR", { status: 500, code: "internal_error" }],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Check your prediction
Today the server doesn't know precondition_failed, so it answers
500 internal_error and the older client says Something went wrong on our
side. After the exercise it answers 412 precondition_failed; the older
client still doesn't know the name, but the status class gives This request
can't be completed as sent, which is the right kind of message. A client that
did switch (code) with no fallback would show nothing at all.
When you must make a breaking change, add the new code alongside the old one,
version the API (/v2/… or a header), and remove the old code only after
callers have moved.
The failure to avoid
Returning stack traces or raw database messages exposes implementation details. Replacing every error with 200 breaks callers’ retry and monitoring decisions.
Compare a handler that sends error.message with this mapping. Trace a database
timeout: the first sends something like connect ETIMEDOUT 10.0.3.7:5432, which
reveals an internal address and gives the caller nothing to act on. This one
sends 500 with internal_error, and the details go to the server's log.
Exercise
Extend the contract
Add precondition_failed with status 412, leaving unknown errors generic.
This is a decision before it's code. Write two sentences on when this service
should answer 409 and when 412, with one example request for each. Then add the
code.
Edit lib/contract.js, then rerun the original cases. If the requested rule
changes an earlier result, update that expected value deliberately. Keep the
other cases. Add the acceptance cases to examples so the preview checks them too.
Input "precondition_failed" must return {"status": 412, "code": "precondition_failed"}.
Your working spaceNext.js · 7 filesimport "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<strong>Review Queue · Practice lab</strong>
</header>
{children}
</body>
</html>
);
}
import Cases from "./Cases.jsx";
import OldClient from "./OldClient.jsx";
export default function Page() {
return (
<main>
<h1>Version a Service’s Error Vocabulary</h1>
<p>
lib/contract.js maps internal codes to public ones. lib/client.js is a
client written before your change.
</p>
<OldClient />
<Cases />
</main>
);
}
import { run, examples } from "@/lib/contract.js";
const text = (value) => JSON.stringify(value) ?? "undefined";
export default async function Cases() {
const results = await Promise.all(
examples.map(async ([input, expected]) => {
// Record the input before running, to show whether run changed it.
const before = text(input);
const actual = await run(input);
return {
input: before,
expected,
actual,
kept: text(input) === before,
pass: text(actual) === text(expected),
};
}),
);
return (
<section>
<h2>Executable cases</h2>
<p>
Checks: {results.filter((row) => row.pass).length}/{results.length}{" "}
passed
</p>
<table>
<thead>
<tr>
<th>Input</th>
<th>Expected</th>
<th>Actual</th>
<th>Input kept</th>
</tr>
</thead>
<tbody>
{results.map((row, i) => (
<tr key={i}>
<td>
<code>{row.input}</code>
</td>
<td>
<code>{text(row.expected)}</code>
</td>
<td className={row.pass ? "pass" : "fail"}>
<code>{text(row.actual)}</code>
</td>
<td className={row.kept ? "pass" : "fail"}>
{row.kept ? "yes" : "no: run changed it"}
</td>
</tr>
))}
</tbody>
</table>
</section>
);
}
import { run } from "@/lib/contract.js";
import { messageFor } from "@/lib/client.js";
const codes = ["forbidden", "conflict", "precondition_failed", "SQL_ERROR"];
export default function OldClient() {
return (
<table aria-label="What an older client shows">
<thead>
<tr>
<th>Server code</th>
<th>Response</th>
<th>Older client shows</th>
</tr>
</thead>
<tbody>
{codes.map((code) => {
const response = run(code);
return (
<tr key={code}>
<td>{code}</td>
<td>
{response.status} {response.code}
</td>
<td>{messageFor(response)}</td>
</tr>
);
})}
</tbody>
</table>
);
}
body {
font: 16px/1.5 system-ui;
margin: 0;
color: #182b3c;
background: #f4f8fc;
}
header,
main {
max-width: 900px;
margin: auto;
padding: 20px;
}
header {
border-bottom: 1px solid #cbd5e1;
}
a {
color: #075985;
}
button,
input,
textarea,
select {
font: inherit;
padding: 8px;
margin: 4px;
}
button {
cursor: pointer;
}
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
padding: 10px;
border: 1px solid #cbd5e1;
text-align: left;
vertical-align: top;
}
pre,
code {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
.pass {
color: #166534;
}
.fail,
[role="alert"] {
color: #a01818;
}
section {
margin: 16px 0;
padding: 12px;
background: white;
border-radius: 8px;
}
nav {
display: flex;
gap: 16px;
}
// A client released before precondition_failed existed. It knows some codes
// by name; for anything else it falls back on the status class.
export function messageFor({ status, code }) {
if (code === "conflict") return "Someone else changed this. Reload and try again.";
if (code === "forbidden") return "You can't do that.";
if (status >= 400 && status < 500) return "This request can't be completed as sent.";
return "Something went wrong on our side. Try again later.";
}
export function run(code) {
const status =
new Map(
Object.entries({
invalid: 400,
unauthenticated: 401,
forbidden: 403,
missing: 404,
conflict: 409,
}),
).get(code) ?? 500;
return { status, code: status === 500 ? "internal_error" : code };
}
export const examples = [
["forbidden", { status: 403, code: "forbidden" }],
["SQL_ERROR", { status: 500, code: "internal_error" }],
];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
412 means "the precondition you sent didn't hold", which is different from
409's "your change conflicts with the current state". It's one more public code
with its own status, added next to the others.
Hint 2: a first step
Do not use 409 for every concurrency problem; an explicit HTTP precondition has its own status.
Show one solution
Replace the exported run function and any constants shown here; keep the
examples export underneath it. Compare the result with your own approach.
JavaScript
export function run(code) { const status = new Map( Object.entries({ invalid: 400, unauthenticated: 401, forbidden: 403, missing: 404, conflict: 409, precondition_failed: 412, }), ).get(code) ?? 500; return { status, code: status === 500 ? "internal_error" : code };}
One map entry adds the code and its status together, and the ?? 500 fallback
still turns anything unlisted into internal_error. Adding a code like this is
backwards-compatible only because clients were told to treat unknown codes by
their status class, as the older client in the lab does. Run the lab again: its
precondition_failed row now shows the 4xx message.
Explain it without the code
Write how a client built before precondition_failed existed should behave the
first time it receives one.
Going further
Read the Next.js guide for the framework boundary around this rule.
The preview runs code-playpad's Next.js simulation. Real caching, hydration,
authentication and persistence require the real-server checks described on Day 7.
Completion is saved in this browser only. It does not measure mastery.