Audit the Wall's Error Responses

Every /problems route now checks its input with validate(), and each refusal goes out through errorBody() in responses.ts as { error: { code, message, details } }, the shape the wall’s API has used for failures since the starting project.

A client that reads one error from this API can read them all, as long as every failure comes back in this shape. The last lesson ended on two that don’t: broken JSON and an unknown path still get Express’s HTML pages.

In this lesson, you’ll collect every failure the API can send today, judge each one by its shape, its status, its code, and what it gives away, and finish with a table of the answers the API should send.

Read the Failures Your Code Answers

A client can only react to an error it can tell apart from the others, so start with the responses you wrote. With the API running, ask for a problem that doesn’t exist:

Language: Shell
$ curl -i http://localhost:3000/problems/42
HTTP/1.1 404 Not Found
...
Content-Type: application/json; charset=utf-8
...

{"error":{"code":"problem_not_found",
"message":"No problem with id 42.","details":null}}

The body is wrapped here for display. The status says what kind of failure this is, and the Content-Type tells the client to parse the body as JSON. Inside it, the message and the code do different jobs.

The message is for a person, and it changes: it holds the ID you asked for, and you might reword it next month. The code is for a program. If Arjun’s front desk page shows “That problem isn’t on the wall” whenever it reads problem_not_found, it keeps working however you rephrase the message. A code that never changes once clients rely on it is a stable code, and clients should branch on it, never on the message text.

The validator’s refusals are the other responses your code writes, and they’re the ones that fill in details. Send a new problem with three broken fields at once: a name that’s only spaces, a pink hold, and a V14:

Language: Shell
$ curl -i -H "Content-Type: application/json" \
  -d '{"name": "  ", "colour": "pink", "grade": "V14",
  "setOn": "2026-11-20"}' http://localhost:3000/problems
HTTP/1.1 422 Unprocessable Entity
...
Content-Type: application/json; charset=utf-8
...

{"error":{"code":"invalid_body",
"message":"Check the fields listed in details.",
"details":{"formErrors":[],"fieldErrors":{
"name":["Too small: expected string to have >=1 characters"],
"colour":["Invalid option: expected one of
\"yellow\"|\"blue\"|\"green\"|\"red\"|\"black\""],
"grade":["Invalid option: expected one of
\"V0\"|\"V1\"|\"V2\"|\"V3\"|\"V4\"|\"V5\"|\"V6\"|\"V7\"|\"V8\"|\"V9\"|\"V10\""]}}}}

The body is wrapped for display, and it already meets the contract: JSON, the { error: { code, message, details } } shape, and a code to branch on, with 422 for a readable body that breaks the rules.

Inside details, fieldErrors has one key per failing field, so a form can put “Invalid option” next to the color picker without parsing the message, and a member can fix every field in one go. A refused query value, such as colour=purple, gets the same kind of answer with 400 and invalid_query, as its row in the table at the end shows.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.