Heuristic:2aronS Duel Agents Fail Fast Error Messages
| Knowledge Sources | |
|---|---|
| Domains | Debugging, Developer_Experience |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
Validate credentials with a strict regex and reject early with an error message that names the failing field, states the expected format, and links to the dashboard where a replacement key can be created.
Description
The Duel codebase never lets an invalid API key reach a network call. `validateApiKey` checks the `duel_<prefix>_<secret>` pattern first, and `requireApiKey` throws a single error string that bundles three pieces of information: the offending variable name (`DUEL_API_KEY`), the expected format, and the dashboard URL where a valid key can be obtained. The same "fail fast, name the fix" pattern appears in the CLI (`promptForKey` warns on a malformed env key before prompting), in `getProxyUrl` (names `DUEL_PROXY_URL`, states the expected shape), and in the SDK (`DuelClient` constructor throws `DuelAuthError` with the dashboard URL). The goal is to turn a cryptic 401 from a remote server into an actionable local error.
Usage
Apply this heuristic whenever you are validating a credential, configuration value, or required argument before making a network call. It is the right choice when a bad input can be detected locally — by regex, type check, or presence — and when a human-readable fix exists (a URL, a format spec, a fallback value).
The Insight (Rule of Thumb)
- Action: Validate the input synchronously, before any I/O, and throw on failure.
- Value: The error message must include (1) the variable/field name, (2) the expected format or shape, and (3) a link to where a valid value can be obtained.
- Trade-off: Slightly stricter upfront validation can reject keys that a lenient check would allow, but this is worth it: a 0ms local error is always cheaper and clearer than a 15s-timed-out network error.
- Compatibility: Works for any credential or config check; does not apply when the only way to know an input is bad is to ask the server (e.g., revoked keys).
Reasoning
A remote 401 tells the user "unauthorized" but not why — the key may be malformed, expired, revoked, or belong to another account. A local regex check can distinguish "malformed" from "rejected by server" and point the user at the dashboard before any network round-trip. In `checkConnectivity`, the invalid-key branch returns immediately with a dashboard URL, while a well-formed-but-rejected key falls through to the network probe that distinguishes `unauthorized` from `unreachable`. This separation shrinks the support surface: the most common error (typo in the key) is handled without a network call.
Code Evidence
Format-then-fail from `packages/core/src/config.ts:23-39`:
export function validateApiKey(plaintext: string | undefined | null): boolean {
if (!plaintext || typeof plaintext !== "string") return false;
return API_KEY_PATTERN.test(plaintext.trim());
}
export function requireApiKey(
input: string | undefined | null,
label = "DUEL_API_KEY",
): string {
const trimmed = input?.trim() ?? "";
if (!validateApiKey(trimmed)) {
throw new Error(
`Invalid or missing ${label}. Get a Duel API key at ${DASHBOARD_URL}. Format: duel_<prefix>_<secret>`,
);
}
return trimmed;
}
Same pattern for proxy URL from `packages/core/src/config.ts:42-53`:
if (!/^https?:\/\/.+/i.test(url)) {
throw new Error(
`Invalid DUEL_PROXY_URL "${url}". Expected an http(s) URL (trailing slashes are stripped automatically).`,
);
}
SDK constructor fails fast from `packages/sdk/src/client.ts:69-73`:
if (!opts?.apiKey) {
throw new DuelAuthError(
"apiKey is required. Get a Duel API key at https://duelagents.com/dashboard/settings",
);
}