How AI code goes wrong
Lesson 21 Fake success
swallowed errors, fallback values, TODOs and logs instead of failures
What you'll learn
- Recognise a catch or except that hides a failure
- Spot stubs, TODOs and defaults that fake success
- Ask whether the caller can tell a failure from an empty result
The scariest AI code never crashes. It seems to work: it returns a nice empty list, an "ok", a default. The failure is hidden.
The idea
AI hates an error. So it adds a try/catch that returns an empty value, a fallback number, a stub that says "ok", or a log line, and the crash goes away. But the problem is still there: you just can't see it. The user sees "no orders" when the server is down, or "paid" when nothing was charged.
The test is simple: after a failure, can the caller tell? If a failed call and a real empty result look the same, that's fake success. Search for catch, except, return [], ?? 0, TODO and pass, and read what happens next.
1async function loadOrders(userId) {2 try {3 const res = await fetch(`${API}/users/${userId}/orders`);4 return await res.json();5 } catch (err) {6 console.log("could not load orders", err.message);7 return [];8 }9}1011async function chargeCard(order) {12 // TODO: call the payment provider13 return { ok: true };14}1516const orders = await loadOrders(42);17if (orders.length === 0) showEmptyState();
- Line 4
fetchdoesn't throw on a 404 or 500. Without ares.okcheck, the error page is read as if it were the orders. - Line 7 The error is logged and thrown away. The caller gets
[]for a failure, the same as for "no orders". - Line 12 A stub that returns success. Nothing is charged, but every order will look paid.
- Line 17 The screen can't tell "no orders" from "broken". The user is told the wrong thing.
Take away
- After a failure, the caller must be able to tell.
- A catch that returns an empty value, a default or "ok" is fake success.
- Search for catch, return [], ?? 0, TODO and stubs, and read what happens after them.
Quiz: test yourself
7 questions: a quick quiz, then a review drill. Pick an answer to see why it's right.