Errors Are Part of Your Library's Public API
Authors spend hours on function names and argument order, then throw whatever the underlying code happened to throw. Yet the moment a user writes a catch block or an except clause around your call, the shape of your errors becomes something they depend on. I think errors deserve the same design attention as return values.
What callers actually rely on
When code handles an error from a library, it typically inspects one of a few things:
- the error's type or class, for
instanceofchecks orexcept SomeErrorclauses; - a code property, such as
err.code === "E_TIMEOUT"; - sometimes, regrettably, the message text, matched with a regular expression.
Each of these is a contract. Change the class, rename the code or reword the message, and some caller's error handling silently stops working. Their program does not crash on upgrade; it crashes later, in the error path that is least likely to be tested.
A design I keep coming back to
For JavaScript and TypeScript libraries, a single base error class with a stable string code works well. Here is an illustrative version for a hypothetical config loader:
export class ConfigError extends Error {
constructor(code, message, options) {
super(message, options); // options.cause keeps the original error
this.name = "ConfigError";
this.code = code; // e.g. "CONFIG_NOT_FOUND", "CONFIG_INVALID"
}
}
throw new ConfigError("CONFIG_INVALID",
`Expected "port" to be a number, got "${typeof port}"`,
{ cause: originalError });
The class gives callers one thing to catch. The code gives them a stable value to branch on. The message is for humans and can be improved freely, because the README tells people not to parse it. The cause option, standard in modern JavaScript runtimes, preserves the underlying error for debugging without making it part of your contract.
In Python the equivalent is a small exception hierarchy: one base exception for the package, and a few subclasses for the cases callers genuinely need to tell apart. Raising with raise ConfigInvalid(...) from exc keeps the original traceback attached.
Documenting errors
A short table in the documentation goes a long way. List each code, when it happens and what the caller can do about it. If a function can fail in several ways, say so next to the function, not in a separate page nobody finds.
When changing errors is breaking
My rule of thumb under semantic versioning:
- Major: removing an error class or code, changing which class is thrown for an existing failure, or turning a thrown error into a returned value (or the reverse).
- Minor: adding a new, more specific subclass that still extends the old one, or adding a new code for a new failure mode.
- Patch: rewording messages, adding a
cause, improving stack traces.
Some maintainers treat message changes as breaking too, because they know users match on them. That is a reasonable stance if you have never offered codes. The better long-term fix is to offer codes, document them, and then reclaim the freedom to write better messages. For related versioning terms, see the glossary.