When it breaks — reading errors, and asking well
By the end of this lesson you will have broken fetch.js on purpose, found the failing line by bisection, and saved a four-part question — error, code, expected, tried — in a file called asking-well.md.
You will spend more time on things not working than on things working. That is not a beginner's condition; it is the job. This lesson makes it survivable.
Read the error from the top
In Node, an error prints the message first, then a stack trace: a list of lines starting with at, with the most recent call on top and the oldest at the bottom. The last line of the whole output is usually just the Node version. Find the first at line that names a file you wrote; lines below it are the chain of calls that led there, and lines naming node:internal are Node's own code. If no at line names your file, use the message and the console.log method below.
Three you will meet constantly:
is not defined— a typo in a name, or you used it before declaring it.Cannot read properties of undefined (reading 'close')— you asked for.closeon something that is not there. Almost always an array index past the end, or a field that was named differently than you assumed.Unexpected token '<', "<!DOCTYPE "... is not valid JSON(orUnexpected token 'F', "Forbidden"...) — you called.json()on something that is not JSON. Usually an error page, which means the request failed and nothing checked.
Each of those points at a specific mistake, and you have met all three already in this course.
Narrow it before you fix it
The instinct is to change things. The faster move is to find out where it goes wrong:
console.logjust above the failing line. Is the value what you expected?- If not, move up and log again. Keep going until the value is right.
- The line between "right" and "wrong" is your bug.
This is bisection, and it turns "it doesn't work" into a specific question in about four minutes.
Asking an assistant well
Now that you have a specific question, the assistant is genuinely good. What makes the difference:
- Paste the actual error, all of it, not a description of it.
- Paste the code around it, not the whole file.
- Say what you expected and what happened instead.
- Say what you already tried.
"It doesn't work" gets you guesses. The four things above usually get you the answer, because they are the same four things a human colleague would ask for.
Read the answer sceptically
It will sound confident either way. Two questions before you accept anything: does this explanation account for the actual error text, and can I say in one sentence why this fixes it?
If you cannot answer the second, you have not fixed the bug — you have changed something and the symptom moved. Build Your First Tool turns this into a rule you will use on every generated change: never commit what you did not understand.
Try it now
Break your fetch script deliberately: remove the res.ok check you added last lesson, misspell the ticker (with the demo key a bad ticker answers 403, not 404), so that .json() runs on an error response, and practise the whole loop: read the error, bisect to the line, then write the question you would ask an assistant, with all four parts. Writing it out is usually where you find the answer yourself.
The prompt
Here the prompt is the lesson itself: the four parts, in order. Fill it in from your broken script, save it as asking-well.md, then paste it into your chat assistant:
Error, in full:
[every line the terminal printed]
The code around it:
[the failing line, and the few lines above it]
What I expected: [one sentence]
What happened instead: [one sentence]
What I already tried: [what I printed, and what it showed]
Explain the cause in one sentence before suggesting any change. Do not
rewrite the whole file.
A good answer gives a cause that accounts for the actual error text — the request was refused, so the body was never JSON — and suggests a change of a few lines, not a new file. Run its change before you trust it: a fix can pass both sceptical questions below and still not run, and "Compare with ours" shows one that does exactly that.
Check it yourself
- The trace may name no line of
fetch.jsat all: in current Node everyatline is inside Node's own code. Above it, Node prints the body it choked on, with a caret under it. This is the case bisection exists for. - A
console.log(res.status)just aboveres.json()prints403or404, not200, and a print just below it never appears. The line between them is the one that throws. - Printing
await res.text()there instead shows the body itself:Forbiddenwith the demo token. - The answer passes both sceptical questions: it accounts for the error text, and you can say in one sentence why its change works.
Compare with ours
Our asking-well.md
Error, in full:
<anonymous_script>:1
Forbidden
^
SyntaxError: Unexpected token 'F', "Forbidden" is not valid JSON
at JSON.parse (<anonymous>)
at parseJSONFromBytes (node:internal/deps/undici/undici:4394:19)
...
The code around it:
const res = await fetch(url);
const bars = await res.json();
What I expected: a list of daily bars.
What happened instead: a SyntaxError on the res.json() line.
What I already tried: console.log(res.status) just above it prints 403.
That error is what Node 24 printed on 6 November 2026; not one line of it names fetch.js, which is why the last line of the question reports the print that does. The cause in one sentence: the server refused the request, so the body is the word Forbidden rather than JSON, and res.ok has to be checked before res.json() runs. The fix is the if (!res.ok) { … } else { … } you wrote in the last lesson, which git restore brings back below.
One plausible fix to refuse: a return; inside the if, outside any function. In this file it breaks everything: Node stops treating the file as one where await may sit at the top, and fails with SyntaxError: await is only valid in async functions and the top level bodies of modules, pointing at the await fetch line you never touched. The else is how this file stops early.
Checkpoint
Put fetch.js back the way it worked with git restore fetch.js (it throws away your uncommitted changes to that one file), so that git diff shows nothing for it, then commit the question:
git status # only the files you expect, and no .env*
git add -A
git commit -m "A four-part question about a 403 read as JSON, kept as a template"
Then answer this lesson's questions.