Skip to content

An Error Response Is a Public Projection

The Course
•
18 min study

The decision

An error can be informative without revealing the exception that caused it. Separate stable caller-facing fields from private diagnostic context and attach a correlation id.

Read, predict, run

The model allowlists code and requestId, ignoring stack and database fields on the input. The transport can return this object while a separate server logger records a redacted event for investigation:

JavaScript
export function run(error) {
  return { code: "internal_error", requestId: error.requestId };
}

Before pressing Run, predict what run returns for {"requestId": "r1", "stack": "private fixture", "database": "fixture"}, from the code above. Write your answers down first: after Run, the case table shows the expected values beside the actual ones.

An Error Response Is a Public Projection Next.js · 5 files
import "./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"; export default function Page() { return ( <main> <h1>An Error Response Is a Public Projection</h1> <p> Read the input and predict the result before running. Change lib/contract.js for the exercise. </p> <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> ); } 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; } export function run(error) { return { code: "internal_error", requestId: error.requestId }; } export const examples = [ [ { requestId: "r1", stack: "private fixture", database: "fixture" }, { code: "internal_error", requestId: "r1" }, ], ];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Check your prediction
  • Input {"requestId": "r1", "stack": "private fixture", "database": "fixture"} → {"code": "internal_error", "requestId": "r1"}.

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.

The failure to avoid

Spreading an Error object is not a redaction strategy, and Error properties do not all serialize the way plain objects do. Make the output shape intentional.

Compare returning the error spread into an object with the allowlist. An error carrying message: "duplicate key value violates unique constraint reviews_pkey" sends the database's message to the browser when spread; the allowlist sends code and requestId only, and the message goes to the log.

Exercise

Extend the contract

For an expected validation error, return its public code invalid_input instead of internal_error. Keep all other fields out.

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 {"requestId": "r2", "kind": "validation", "message": "private"} must return {"code": "invalid_input", "requestId": "r2"}.
Your working space Next.js · 5 files
import "./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"; export default function Page() { return ( <main> <h1>An Error Response Is a Public Projection</h1> <p> Read the input and predict the result before running. Change lib/contract.js for the exercise. </p> <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> ); } 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; } export function run(error) { return { code: "internal_error", requestId: error.requestId }; } export const examples = [ [ { requestId: "r1", stack: "private fixture", database: "fixture" }, { code: "internal_error", requestId: "r1" }, ], ];
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Add and remove files here; write their code in the editor.
Hint 1: the idea

The public code is chosen from a trusted classification of the error, not copied from it. A validation failure is one class the caller can fix, so it earns its own code.

Hint 2: a first step

Use a trusted classification supplied by your error adapter; never copy arbitrary messages into the public result.

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(error) {
  return {
    code: error.kind === "validation" ? "invalid_input" : "internal_error",
    requestId: error.requestId,
  };
}

error.kind is set by the server's own error adapter, so it's safe to branch on; error.message is never copied. Everything except code and requestId stays out. A caller can now tell "fix your input" from "try later" without learning anything about the implementation.

Explain it without the code

Write the support conversation a requestId makes possible, and what the server log must contain for it to work.

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.

Was this article helpful?

Your response is saved on this device.