Agent Wranglers

ARC-31Required

Never swallow errors

Never catch an error and carry on as if nothing happened. Handle it, add context and rethrow it, or let it reach the edge.

How to do it

Catch only errors you can do something about. When you rethrow, keep the original as the cause. Log once, at the edge, with a reference the user can quote. A swallowed error in an access check must deny, as Deny access when a check fails says.

Examples

TypeScript example

// Bad: the order is lost and nobody knows
try { await saveOrder(order); } catch {}

// Good: add context and keep the cause
try {
  await saveOrder(order);
} catch (error) {
  throw new OrderNotSaved(order.id, { cause: error });
}

Python example

try:
    save_order(order)
except DatabaseError as error:
    raise OrderNotSaved(order.id) from error

Java example

try {
    saveOrder(order);
} catch (SQLException e) {
    throw new OrderNotSaved(order.id(), e);
}

How to check

A linter flags empty catch blocks and broad catches. Examples are ESLint's no-empty, Ruff's BLE001 and S110, and PHP_CodeSniffer's Generic.CodeAnalysis.EmptyStatement.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/architecture/errors-and-nulls/never-swallow-errors">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.