Contents Lesson 8 of 16

10 min read · professional

Errors you can see from outside

By the end of this lesson you will have a health route that costs no calls and reports the store and upstream separately, one line of JSON in the server log for every upstream error with no URL in it, and a panel crash that reaches that log instead of dying in a closed tab.

A console.log is a diagnostic for someone sitting in front of the machine. Once the tool is deployed, nobody is.

Two audiences, two channels

The user needs to know what to do. Build a Dashboard settled this: five error kinds, five messages, and 402 never rendered as "something went wrong".

You need to know it happened at all. That is a different channel, and it must reach you without anyone reporting it. A tool whose failures are only visible to the person suffering them is a tool that stays broken.

Log on the server, structured, without secrets

console.error(JSON.stringify({
  event: "proxy_upstream_error",
  status: res.status,
  path: upstream.pathname,     // never the full URL — the key is in it
  ts: Date.now(),
}));

Three rules and each has a specific reason. Structured, not prose, because you will want to search it. Never the full upstream URL, because the key is a query parameter on it and logs are the second most common way keys leak after bundles. The path, not the body, because the body is data you may not redistribute.

What is worth an alert to you

Not everything. Four things:

  • A 402, because it means the wall arrived and you want to know when.
  • A spike in 5xx, because upstream is broken and your tool looks broken.
  • The store failing, because saves are silently not happening.
  • The app failing to boot, which the deploy should have caught and sometimes does not.

Everything else goes in the log and waits until you look.

A health endpoint you can actually use

GET /api/health -> { ok: true, store: "up", upstream: "up", calls_used: 340 }

Two properties matter. It must not need the API key to answer — if checking health spends quota, you will stop checking. And it must report the store separately from upstream, because those fail for different reasons and need different responses from you.

This is what unit 3's monitoring watches, and it is ten lines.

Errors in the browser too

Build a Dashboard put an error boundary around each panel. Wire those boundaries to report — a small endpoint that takes a message and a stack is enough. Without it, a render bug in one panel is invisible to you forever, because the person who hits it just closes the tab.

Try it now

Break each of the four alertable things on purpose — exhaust your quota, stop the store, throw at boot — and confirm you find out without looking at the screen. The one that does not reach you is the one that will happen while you are away.

The prompt

Mock mode on, plan mode on, and paste:

Three changes, tests first wherever there is logic. One: a logEvent
function in src/lib/log.ts that writes one line of JSON per event; use
it in src/lib/proxy.ts for every upstream status of 400 or more, with
the status and the path only, never the URL or the body. Two: a GET
route in src/app/api/health that answers ok, store (a read succeeds and
its file is still where it was opened) and upstream separately,
calls_used and daily_limit, and
503 when anything is down. It may use the user endpoint and nothing
else upstream. Three: a POST route in src/app/api/client-error that
takes a message and a stack, cuts both short, logs them, and is
limited per address like the proxy; call it from
src/components/PanelBoundary.tsx when a panel crashes. Tell me what
one health check costs before writing it. Done means npm test,
npm run typecheck, npm run lint and npm run check:leak green.

A good answer prices a health check at no calls, adds src/lib/log.ts, the two routes and their tests, and changes src/lib/proxy.ts, the store, for the readable check, and src/components/PanelBoundary.tsx. The health answer carries a count and never the account's e-mail address, which the user endpoint also returns.

Check it yourself

  1. In mock mode, curl -s localhost:3000/api/health answers ok with store and upstream up, and curl -s localhost:3000/api/usage reads the same count before and after ten health checks. In mock mode the count comes from a fixture and never moves, so this only proves something against your live key.
  2. The health test makes the store unreadable and gets 503 with the store down and upstream up. On macOS or Linux, see it live as well: rename the store's data folder while the server runs and ask again, then rename it back. An open file survives a rename there, which is why the route looks for the file and not only for a working read. On Windows the rename is refused while the server holds the file, so the test is your evidence.
  3. Ask the proxy for a symbol your fixtures do not have, with curl -s localhost:3000/api/proxy/eod/X (the end-of-day path; the real-time path answers a mock quote and logs nothing): the terminal running npm run dev, not the browser, shows one JSON line with the status and the path, and no api_token anywhere in it.
  4. Make one panel throw while it renders: the panel shows its error state, and the terminal shows a line with the message — the crash reached you without anyone reporting it.
Compare with ours
// src/app/api/health/route.ts: costs no calls, and says which half is down
export async function GET() {
  const store = await storeReadable();          // one read from Build a Dashboard's store, and its file still there; no write
  const res = await eodhdFetch("user", {}).catch(() => null);   // the free endpoint
  const u = res?.ok ? ((await res.json()) as UserAnswer) : null;
  const body = {
    ok: store && u !== null,
    store: store ? "up" : "down",
    upstream: u ? "up" : "down",
    calls_used: u?.apiRequests ?? null,
    daily_limit: u?.dailyRateLimit ?? null,
  };
  return Response.json(body, {
    status: body.ok ? 200 : 503,
    headers: { "cache-control": "no-store" },
  });
}

// src/lib/proxy.ts, once the upstream answer is in
if (answer.status >= 400) logEvent("proxy_upstream_error", { status: answer.status, path: target });

The full state at the end of this unit is on the starter's branch ref/ship-your-tool/u2.

The reference dashboard at the end of unit 2, in mock mode

Checkpoint

npm test && npm run typecheck
git status          # the files you expect, and no .env file
git add -A
git commit -m "Health that costs no calls, server logs without the URL, panel crashes reported"

Then answer this lesson's questions.