Contents Lesson 14 of 21

9 min read · foundations

Narrowing — the smallest thing that still breaks

By the end of this lesson you will have fetch.js printing the address, the status and the raw body on three labelled lines, and three breaks, each caught by the print you predicted — or a prediction you now know was wrong.

The method in the last lesson assumes you know roughly where to look. Often you do not. Something in forty lines is wrong and every line looks reasonable.

Narrowing is how you go from "somewhere in here" to "this line" without reading forty lines forty times.

Halve it

Disable half the program. Does it still break?

If yes, the problem is in the half that still runs. If no, it is in the half you disabled. Either way you just threw away half the search space with one run. Do it again. Forty lines becomes twenty, then ten, then three, in about five steps.

This works because it does not require you to understand the code. It only requires you to run it.

Build the smallest thing that still breaks

Now go the other way. Start a new empty file and add back the minimum needed to reproduce the failure. Strip until removing one more thing makes it work.

What you are left with is two valuable things at once. It is the answer — the failure lives in the last piece you could not remove. And it is the best possible question, because "these six lines produce this error and I expected this" is something an assistant, a colleague, or a search engine can actually answer. "My app is broken" is not.

A worked example

Your request from the last unit stops returning prices. Three beliefs, three checks, in order:

const token = "demo";
const symbol = "AAPL.US";
const url = `https://eodhd.com/api/eod/${symbol}?api_token=${token}&fmt=json&from=2026-08-20`;
console.log("url:", url.replace(token, "***"));  // 1. is the address what I think it is?

const res = await fetch(url);
console.log("status:", res.status); // 2. did the server accept it?

const body = await res.text();
console.log("body:", body);         // 3. what actually came back?

Two new pieces of syntax. A string in backticks (the key left of 1 on most keyboards) may contain ${name}, which is replaced by that variable's value: it is how one address is built from a symbol and a key. A string in ordinary quotes does not do this, and would send the characters ${symbol} to the server. And url.replace(token, "***") makes a copy of the address with the key hidden, so the print never shows it.

Each print kills a whole class of possibility. A URL with undefined in it means the failure is upstream, where the symbol was set. A status of 401 means the key, not the code. A body that is HTML rather than JSON means you got an error page and the parse was never going to work.

Notice the third one uses .text() rather than .json(). Parsing throws away the evidence: if the body is not JSON, .json() fails with a message about position 0, which tells you nothing about what the body actually was. Look at the raw text first. There is no second look, either: a response body can be consumed only once, so after .json() has thrown, asking the same response for .text() gets you an error rather than the page.

Narrow the data, not just the code

The most common wasted afternoon is reading correct code very carefully while the input is wrong. If the code looks right, stop reading it and print what is going in. Where does this value come from? Is it the shape I assumed?

Try it now

Take your request script and break it three ways, one at a time: a misspelled symbol, a wrong key, and a URL missing fmt=json. Before fixing each, predict which of the three prints will show the problem. Being wrong about that prediction is the useful part.

The prompt

Rebuild fetch.js yourself from the worked example: move the key and the symbol into the two variables token and symbol, build url from them in backticks, and print the three labelled lines (address with the key hidden, status, raw body) instead of parsing the response. Drop the res.ok check for this lesson; the prints do its job. Make one break, and run it. Then paste what it printed into your chat assistant:

My script prints three labelled lines before it parses anything: the
address with the key hidden, the status, and the raw body. I broke it
in one way, and this is what it printed. Do not tell me the fix. Tell
me which of the three prints shows the fault, and what each of the
other two rules out.

[paste the three printed lines]

A good answer names one print and says what the other two clear. A well-formed address and a 401 status, for instance, clear the address and point at the key.

Check it yourself

  1. A misspelled symbol on the demo token: status 403, body Forbidden.
  2. A wrong key: status 401, body Unauthenticated.
  3. No fmt=json: status 200, and a body that starts Date,Open,High,Low,Close — comma-separated text, not JSON. Only the body print shows this one.
  4. The address line never shows the key: three asterisks stand where it was.

Compare with ours

Our fetch.js, and what each break printed
const token = "demo";
const symbol = "AAPL.US";
const url = `https://eodhd.com/api/eod/${symbol}?api_token=${token}&fmt=json&from=2026-08-20`;
console.log("url:", url.replace(token, "***"));

const res = await fetch(url);
console.log("status:", res.status);

const body = await res.text();
console.log("body:", body.slice(0, 120));

Measured on 6 November 2026:

Break Status Body starts The print that shows it
misspelled symbol, demo token 403 Forbidden status
a key that does not exist 401 Unauthenticated status
fmt=json left out 200 Date,Open,High,Low,Close,… body

body.slice(0, 120) prints only the first 120 characters of the body. A working request returns months of bars on one line, and the start is enough to tell JSON from an error or from comma-separated text.

The third is the one people lose an afternoon to. Every print but the last says success, and only the raw body shows it was never JSON.

Checkpoint

Undo the three breaks and keep the prints:

git status   # only the files you expect, and no .env*
git add -A
git commit -m "Print address, status and raw body before parsing, so a failure names its layer"

Then answer this lesson's questions.