Basic

Responses are built, not returned

The handler gives a value and the framework turns it into a response. The turn is where the status and the type are decided, and the decision is the same for every route unless it is made explicit.

What you will see

One line. The status is written, never defaulted.

About 1 min of reading

Try It

Write one error builder

1

Write the error shape

printf 'error field allowed reason\n' > /tmp/e.txt; cat /tmp/e.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line of three names. Three names is what makes an error fixable.
2

Fill it for a bad field

printf 'error field=color allowed=name,version reason=unknown\n' > /tmp/e2.txt; cat /tmp/e2.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line with both lists. Both lists is the shape every route uses.
3

Write the status explicitly

printf 'status 400\n' > /tmp/s.txt; cat /tmp/s.txt

Before you run it, say out loud what it will print. Then open the answer.

What it prints
One line. The status is written, never defaulted.

Data Flow

A handler value to a response
1 The handler decides the outcome
↓
2 The builder is called with the outcome
↓
3 The status and type are written
↓
4 The error names the field and the allowed set
Why this works
Why the status is always explicit

An explicit status is a claim. A default status is a claim nobody wrote, and nobody can argue with a claim nobody wrote.

The one thing the builder must not do

It must not decide the cause. The handler knows the cause, and the builder only writes it.

Examples

The default is a lie

A value returned with no status is a 200. A 200 for something that did not happen is a response that lies.

Errors are a shape

An error response names what failed and what was allowed. The two names are what make the error fixable.

One builder for all routes

The shape of an error is the same everywhere. A shape written per route is a shape that will differ.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← The request is an object with rules
Previous Lesson
The request is an object with rules
Next Lesson
Application state and its owner
Application state and its owner →