Skip to content

Lesson 8 of 14 · 20m

The Error Path Is a Real Path

Failures don't queue politely — classify caller, transient, and bug errors, and design what happens on each path.

Run JSON.parse(req.body) on a public endpoint. Most days it parses. The day it doesn't, it throws a SyntaxError — and if nothing caught it, Express answers 500, or the process dies. Since Node 15, an unhandled promise rejection crashes the process by default; before that it printed a warning everyone ignored, which is exactly why the default changed. The happy path is one path among many. Failures don't queue politely waiting for you to notice.

The two wrong mental models: "catch everything to be safe" — try { ... } catch (e) {} converts loud failures into silent wrongness, and silent-wrong is the most expensive state a system can be in — and "errors are exceptional, edge events." At a million requests a day, a 1-in-10,000 failure is a hundred daily events.

Classify first, handle second

Errors sort into three kinds, and each wants a different response:

  • Caller errors — bad input, malformed JSON, missing field. Deterministic, the caller's fault: answer a 400-class status with a message that says what's wrong. Never retry; retrying bad input just fails slower.
  • Transient/system errors — a timeout, ECONNRESET, a 503 from upstream. Worth retrying, with exponential backoff and jitter — Marc Brooker's 2015 AWS architecture post on backoff-and-jitter is the canonical write-up, because retries synchronized across clients become a second outage.
  • Programmer errors — null dereference, violated invariant, unreachable code that was reached. This is a bug: fail fast and loud so it surfaces in monitoring. Don't retry a bug.

Retry storms are a second outage

"Just retry it" deserves precision, because naive retries amplify a failure. Exponential backoff — wait 1s, then 2s, then 4s — caps how hard you hammer a struggling upstream. Jitter — randomizing each client's wait — is what stops a thousand clients who all failed at the same instant from retrying at the same instant. Without it, your retry policy becomes the second outage.

Errors as values vs. exceptions

Go made the error a return value — if err != nil looks verbose until you realize the verbosity is the point: handling is a decision you make at each hop, in the same syntax as the rest of the code. Rust's Result<T, E> with the ? operator makes ignoring an error a compile-time decision you have to write down as .unwrap(). C's errno and unchecked scanf return values are the historical counterexample — errors exist but the language won't make you look at them, which is how silent wrongness happens. Java's checked exceptions were the same idea done loudly; the debate about them is itself the lesson: the contract exists whether you design it or not.

Write the message for the person debugging it

An error message has an audience, and it isn't the user — it's whoever is debugging at 3am, which is usually you. ECONNREFUSED 10.0.0.4:5432 tells an operator exactly what to check; Internal server error tells them nothing. Same failure, different incident length. Actionable errors are a feature: "retry in 30s," "token expired — re-authenticate," "field email must be a valid address."

The error path needs tests too

It isn't handled until you've seen it fail correctly. Mock the database to throw, use nock or msw to fail the HTTP call, and assert the classified response — the 400 for bad input, the retry for the transient, the surfaced error for the bug. "It threw something" is not a test result.

Try it yourself — classify, in JS

function handle(e) {
  if (e.name === "SyntaxError") return { status: 400, retry: false };   // caller error
  if (e.code === "ECONNRESET")  return { status: 503, retry: true };    // transient
  return { status: 500, retry: false, alert: true };                    // bug: surface it
}
console.log(handle(new SyntaxError("bad json")));  // -> 400, don't retry
console.log(handle({ name: "Error", code: "ECONNRESET" })); // -> 503, retry

The practice

Classify every error your code can produce, design the response per class, and make messages actionable — "retry in 30s," not "oops." catch is where behavior goes to be designed, not deleted.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.