Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Propagation
- Passing a failure from a callee to its caller, layer by layer, until something handles it.
- Unwinding
- Leaving active call scopes as an exception propagates toward a handler; applicable cleanup still runs.
- Context
- Information a layer adds to a failure, such as which hedge it was pricing.
- Cause
- The original failure kept inside a wrapping one, so callers can still inspect it.
- Error cause
- The cause option of new Error(message, { cause }), which keeps the original error.
Passing a failure through callers
Propagation passes a failed operation’s outcome through the calls that depend on it. Each layer must preserve enough information for the eventual handler. Context describes what that layer was attempting; a retained cause identifies the underlying failure independently of its displayed message.
A throw leaves intervening calls until a matching catch handles it; applicable finally blocks still run. An Error can carry a cause value separately from its message, allowing a layer to add context without reducing the original failure to text.
01On the way out
Which completion lines survive a failed price lookup?
A garden-planning tool quotes each hedge from its plant and length. An unlisted plant must leave a useful failure at the quote screen. The price lookup knows which plant is missing; the hedge layer knows which hedge was being priced. The report needs both pieces.
The quote screen must distinguish a completed hedge price from a call that never returned a price. The reduced plan requests box, then holly, and each successful hedge announces itself before the total. Trace the call that fails.
perMetre throws; hedge and plan have no try; main catches.
function perMetre(plant: string): number {
if (plant !== "box") throw new Error(`no price for ${plant}`);
return 4;
}
function hedge(plant: string, metres: number): number {
const cost = perMetre(plant) * metres;
console.log(`priced ${plant}`);
return cost;
}
function plan(): void {
console.log(`total ${hedge("box", 3) + hedge("holly", 2)}`);
}
try {
plan();
} catch (error) {
console.log(`failed: ${(error as Error).message}`);
}
npx tsc main.ts --strict --target ES2022 --module Node16 --moduleResolution Node16 --types node --outDir out && node out/main.js
Use a fresh directory and replace main for each probe. For TypeScript, set package.json’s type field to commonjs while preserving its other fields, use the installed TypeScript and Node types, and run the guarded emit command shown. The Go and Rust commands use the supplied single main file. These examples make ordinary synchronous calls.
02Lost on the way
A middle layer that turns the failure into a value hides it from everyone above
A proposed fallback keeps the quote printable by treating an unlisted plant as cost 0. Try that change in the reduced two-hedge plan and inspect what the report can now distinguish.
hedge wraps perMetre in try and returns 0 in the catch.
function perMetre(plant: string): number {
if (plant !== "box") throw new Error(`no price for ${plant}`);
return 4;
}
function hedge(plant: string, metres: number): number {
try {
return perMetre(plant) * metres;
} catch {
return 0;
}
}
try {
console.log(`total ${hedge("box", 3) + hedge("holly", 2)}`);
} catch (error) {
console.log(`failed: ${(error as Error).message}`);
}
03Context and cause
Does a contextual message still carry the original cause?
The report also needs the failing hedge’s name. This attempt puts “pricing north” and the old message into a new failure. The observer separately checks whether a typed UnknownPlant cause is still attached. Predict that check.
The catch builds a new Error from text. The observer safely handles an absent cause.
class UnknownPlant extends Error {}
function perMetre(plant: string): number {
if (plant !== "box") throw new UnknownPlant(`no price for ${plant}`);
return 4;
}
function hedge(name: string, plant: string, metres: number): number {
try {
return perMetre(plant) * metres;
} catch (error) {
throw new Error(`pricing ${name}: ${(error as Error).message}`);
}
}
try {
hedge("north", "holly", 2);
} catch (error) {
const cause = (error as Error).cause;
console.log((error as Error).message);
console.log(`cause: ${cause instanceof Error ? cause.message : "none"}`);
console.log(`unknown plant: ${cause instanceof UnknownPlant}`);
}
04Repair the hedge planner
Let failures through, add context, handle the right one
The garden planner needs three focused repairs. Keep a missing price visible at the report, attach the current hedge’s name without losing its cause, then apply the box fallback only to an unknown plant. A bad length still belongs in the failure report.
Each ticket is a complete file with its supplied reader and caller. Repair the named pricing layer and check the report for each request.
Repair the hedge planner
Preserve a failure, attribute it to the current hedge, then choose which failure this layer can recover from.
Sign in to keep your read marks on every device. Sign in
Was this helpful?