Topic 1 of 5 · 5 min read
Swallowed errors and silent fallbacks
Goal: Find where AI code hides a failure behind a catch-all or default value and decide how the failure should surface.
A failure that nobody can see is worse than a crash: AI code often turns errors into quiet default values, and the bug moves from a stack trace to a wrong number in production.
Check yourself · 1 of 3
An AI-written function returns 0 when the ledger call fails with any exception. What is the main risk?
Why it matters
Coding assistants such as Claude Code, Copilot, Cursor and Codex are asked to "make it work" and "make the test pass". The shortest way to get green is to wrap the risky line in try/except and return something harmless. The code looks defensive. In fact it has removed the only signal that something broke.
How to find it
Search the diff for these shapes:
- Catch-all handlers.
except Exception,catch (e) {},.catch(() => null). Ask: which specific errors are expected here? - Defaults that look like data.
return 0,return [],return {}in an error branch. The caller cannot tell "no orders" from "the database was down". - Log and continue. A
console.log(err)followed by normal flow. Logs are not handling: nobody is paged and the user sees success. - Optional chaining as armour.
user?.account?.id ?? "unknown"can hide a missing object that should never be missing.
For each one decide the right outcome: retry, return a typed error, show the user a specific message, or let it crash so the supervisor restarts the process. Catch only what you can actually handle, and add context when you re-raise.
Before and after
def get_balance(account_id):
try:
return ledger.fetch(account_id)["balance"]
except Exception:
return 0
A ledger outage now shows every customer a zero balance. The reviewed version names the expected failure and lets the rest propagate:
def get_balance(account_id):
try:
return ledger.fetch(account_id)["balance"]
except LedgerTimeout as err:
raise BalanceUnavailable(account_id) from err
The caller can now show "Balance is temporarily unavailable, try again", and monitoring sees BalanceUnavailable.
What goes wrong
- Tests pass for the wrong reason. The agent hides the exception that made the test fail.
- Corrupt data spreads. A fallback of
0or an empty string is stored, then used downstream. - No alert fires. Error-rate dashboards stay flat because nothing is raised.
- Secrets leak the other way. Over-correcting by returning the raw exception to users exposes internals, so map errors to safe messages.
Examples
The zero that cost a day
async function getStockLevel(sku: string): Promise<number> {
try {
return await warehouse.stock(sku);
} catch {
return 0;
}
}
When the warehouse API times out, every product shows "out of stock" and the shop stops selling. Return a typed error, or return null and make the UI say "availability unknown".
Log and continue
try {
await sendReceipt(order);
} catch (err) {
console.error(err);
}
res.json({ ok: true });
The customer gets ok: true and no receipt. Queue the receipt for retry, or return a response that says the receipt is pending.
Key points
- Catch only the specific errors you can handle; let everything else propagate.
- A default value in an error branch must never be indistinguishable from real data.
- Logging an error and continuing is not handling it: nobody is alerted and the user sees success.
- When you re-raise, add context and keep the original cause.
- Treat a diff that makes a failing test pass by adding a catch as a red flag.
Common mistakes
- Accepting
except Exceptionbecause "it is safer" than crashing. - Reviewing the happy path and only glancing at the
catchblock. - Fixing a swallowed error by logging it, with no change in what the caller receives.
- Returning raw exception text to the end user instead of a mapped, specific message.
Check yourself · 2 of 3
Which handler is the best replacement for a bare catch-all around a payment API call?
Check yourself · 3 of 3
A diff makes a failing test pass by wrapping the failing line in try/except and returning None. What do you do in review?
Practice items, not counted towards your mastery.
Start here
- Making retries safe with idempotent APIs (Amazon Builders' Library) (opens in a new tab)ArticleFree
How idempotency tokens let a client retry a request without doing the work twice.
- Timeouts, retries and backoff with jitter (AWS Builders' Library) (opens in a new tab)ArticleFree
Why every remote call needs a timeout and how capped backoff with jitter avoids retry storms.
- Error Handling Cheat Sheet (OWASP) (opens in a new tab)DocsFree
Handle errors centrally, fail safely and log enough detail without leaking it to users.