Heuristic:2aronS Duel Agents Catch By Category Not Message
| Knowledge Sources | |
|---|---|
| Domains | Error_Handling, Debugging |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
Branch on a typed error class (`instanceof DuelAuthError`) or a numeric HTTP status, never on a substring of the error message — message text changes across versions and locales.
Description
The Duel SDK raises a small, closed hierarchy: `DuelError` (base) → `DuelAuthError` (401/403) and `DuelApiError` (other non-2xx). Callers are expected to discriminate with `instanceof` or by reading the `status` field, not by parsing `err.message`. Inside `DuelClient.request`, the classification is done by HTTP status (`res.status === 401 || res.status === 403` → `DuelAuthError`; `!res.ok` → `DuelApiError`), so the error class is a function of the response, not of a free-text message. This means a caller can write `if (err instanceof DuelAuthError) { /* re-prompt for key */ }` and that branch survives a rewording of the message in a future release.
Usage
Use this heuristic whenever you are handling errors from a library you do not control, or when you are designing an error API for others to consume. It is the right choice when the caller needs to react differently to categories of failure (auth vs. server vs. network) rather than to specific wording. Do not use it when the only signal you have is a message string — in that case, pin the exact message and version and add a test that fails on rewording.
The Insight (Rule of Thumb)
- Action: Throw typed error subclasses with a stable `name` and a numeric `status`; branch on `instanceof` or `status`.
- Value: `DuelAuthError` for 401/403, `DuelApiError` for other non-2xx, each carrying `status` (and `body` for `DuelApiError`).
- Trade-off: Slightly more classes to maintain than a single `Error` with a code field, but `instanceof` survives minification and transpilation in a way that `err.code` does not always.
- Compatibility: Pair with Heuristic:2aronS_Duel_Agents_Fail_Fast_Error_Messages — the typed class carries a human message, but the message is for display, not for branching.
Reasoning
String-matching an error message is brittle: the message can be reworded, localized, or truncated, and a `startsWith`/`includes` check will silently stop matching. A typed class is a stable contract: the class name is set on the instance (`this.name = "DuelAuthError"`) and `instanceof` checks the prototype chain, which is unaffected by message edits. Carrying the numeric `status` alongside the class gives callers a second, machine-precise axis for finer branching (e.g., "retry on 5xx, do not retry on 4xx"). The hierarchy is deliberately two-level (`DuelError` → two leaves) so a `catch (err)` that only wants "is this a Duel error?" can `instanceof DuelError`, and a handler that cares about auth can `instanceof DuelAuthError`.
Code Evidence
Classification by status, not message, in `packages/sdk/src/client.ts:149-162`:
if (res.status === 401 || res.status === 403) {
throw new DuelAuthError(
`Duel API rejected your key (${res.status}). Check DUEL_API_KEY at https://duelagents.com/dashboard/settings`,
res.status,
);
}
if (!res.ok) {
throw new DuelApiError(
`Duel API error ${res.status}`,
res.status,
parsed,
);
}
Typed hierarchy with stable names in `packages/sdk/src/errors.ts:1-31`:
export class DuelError extends Error {
constructor(message: string) {
super(message);
this.name = "DuelError";
}
}
export class DuelAuthError extends DuelError {
readonly status: number;
constructor(message: string, status = 401) {
super(message);
this.name = "DuelAuthError";
this.status = status;
}
}