Naming your errors
Error names are read at the worst possible moment by someone with no context. Most of them are useless.
An error name is read by someone at 3 a.m., in a log aggregator, out of context, in a hurry. It is the highest-stakes identifier in your codebase and it is usually named in three seconds.
the failure patterns#
The generic. Error, AppError, ServiceException. Carries no information. Grep for it and you get everything.
The layer. DatabaseError, NetworkError. Says where it came from, not what went wrong or what to do. A DatabaseError might be a syntax error in your query, a constraint violation caused by user input, or a failover in progress — three completely different responses.
The stringly-typed. One exception type, everything in the message. Now callers parse messages, and you can never reword one.
The internal. RecordNotFoundInCacheOrPrimaryAfterRetryException. Describes the implementation. When the implementation changes, the name lies.
what a good error name encodes#
The category — is this the caller's fault, ours, or transient? This drives the response and should be visible without reading documentation.
The specific condition — precise enough to distinguish from its siblings.
Stability — a name callers can branch on and that will not change meaning.
# what happened, from the caller's perspective, specifically
class InsufficientStock(InvalidRequest): ...
class SkuNotFound(InvalidRequest): ...
class PaymentDeclined(InvalidRequest): ...
class PaymentProviderUnavailable(Transient): ...
class ReservationLockTimeout(Transient): ...The base classes carry the category, so a caller can write except Transient: retry() without knowing every leaf. The leaves carry the specifics, so a caller that cares can distinguish InsufficientStock from SkuNotFound — one offers a backorder, the other is a bug.
the test#
Could someone act on this without reading the message?
PaymentDeclined — yes. Show the user, offer another card. PaymentError — no. Retry? Tell the user? Page someone? Unknowable.
Would two different causes produce the same name? If yes, split it. The whole value is distinguishing cases that need different handling.
Does the name still make sense if you rewrite the implementation? RedisConnectionRefused becomes wrong the day you switch cache. CacheUnavailable does not.
the message, separately#
The name is for code. The message is for a human, and it has different rules:
raise InsufficientStock(
f"SKU {sku} has {available} available, {requested} requested"
)Include the values. An error that says "insufficient stock" without the numbers forces the reader to go and find them. Including them turns a twenty-minute investigation into a glance.
Do not put the fix in the message if it belongs in documentation, but do put it there if it is short and actionable. "Set MAX_BATCH below 500" is worth six words.
Never put a value you cannot log. Credentials, tokens, personal data. Errors end up in log aggregators, error trackers, and support tickets — treat the message as public.
the code, for anything crossing a boundary#
For errors leaving your process, a stable machine-readable code alongside the name:
{ "code": "insufficient_stock", "message": "SKU X-42 has 3 available, 12 requested" }The code never changes meaning. The message can be reworded, translated, or improved freely. Collapsing these two is why so many APIs can never fix a typo in an error message — someone, somewhere, is matching on the string.
the aggregation payoff#
Well-named errors make your error tracker useful rather than a wall of noise.
InsufficientStock: 14,203 occurrences is a product signal — probably an inventory sync problem, possibly a UI that lets people order what is not there.
Error: 14,203 occurrences is nothing. Somebody has to open individual events and read messages to find out that there are actually six unrelated problems grouped together.
The difference between those two dashboards is entirely in the naming, and the naming took the same amount of time.
— Dom, October 21, 2026