Implementation:2aronS Duel Agents Duel Error Hierarchy
| Knowledge Sources | |
|---|---|
| Domains | Error_Handling, API_Design |
| Last Updated | 2026-10-27 14:00 GMT |
Overview
Concrete tool for classifying Duel SDK HTTP failures into a typed error hierarchy (DuelError, DuelAuthError, DuelApiError) that enables category-based catching, provided by @duel-agents/sdk.
Description
The Duel Error Hierarchy is a three-class tree in packages/sdk/src/errors.ts:
DuelError— base class for all SDK-specific errors; extendsErrorwithname = "DuelError". Callers can catch this to filter only SDK-originated errors.DuelAuthError— extendsDuelError; thrown for HTTP 401/403 (key rejected). Carriesstatus: number(default 401).DuelApiError— extendsDuelError; thrown for other non-2xx responses. Carriesstatus: numberandbody: unknown(the parsed response body, or raw text if parse failed).
The DuelClient constructor also throws DuelAuthError if apiKey is missing. The createChatCompletion method throws DuelApiError (status 400) if stream: true.
All three classes are re-exported from @duel-agents/sdk and from packages/sdk/src/client.ts.
Usage
Import these classes when you need to:
- Catch authentication failures separately from API errors
- Access the parsed response body on an API error
- Catch all SDK errors with a single
instanceof DuelErrorcheck
Code Reference
Source Location
- Repository: Duel-Agents
- File:
packages/sdk/src/errors.ts(lines 1-31)
Signature
/** Base class for all errors thrown by the Duel SDK. */
export class DuelError extends Error {
constructor(message: string);
name: "DuelError";
}
/** Thrown when the API rejects the key (HTTP 401 or 403). */
export class DuelAuthError extends DuelError {
readonly status: number;
constructor(message: string, status?: number); // default 401
name: "DuelAuthError";
}
/** Thrown for other non-2xx responses; carries the status and parsed body. */
export class DuelApiError extends DuelError {
readonly status: number;
readonly body: unknown;
constructor(message: string, status: number, body: unknown);
name: "DuelApiError";
}
Import
import { DuelError, DuelAuthError, DuelApiError } from "@duel-agents/sdk";
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| message | string |
Yes | Error message |
| status (DuelAuthError) | number |
No | HTTP status (default 401) |
| status (DuelApiError) | number |
Yes | HTTP status |
| body (DuelApiError) | unknown |
Yes | Parsed response body |
Outputs
| Name | Type | Description |
|---|---|---|
DuelError instance |
Error subclass |
name = "DuelError"
|
DuelAuthError instance |
DuelError subclass |
name = "DuelAuthError", status
|
DuelApiError instance |
DuelError subclass |
name = "DuelApiError", status, body
|
Usage Examples
Catch by Category
import { DuelAuthError, DuelApiError, DuelError } from "@duel-agents/sdk";
try {
const result = await duel.chat.completions.create(params);
} catch (e) {
if (e instanceof DuelAuthError) {
// Key rejected — prompt for a new key
console.error(`Auth failed (${e.status}): ${e.message}`);
promptForNewKey();
} else if (e instanceof DuelApiError) {
// API error — inspect status and body
console.error(`API error ${e.status}:`, e.body);
} else if (e instanceof DuelError) {
// Catch-all for other SDK errors
console.error(`SDK error: ${e.message}`);
}
// Non-SDK errors (e.g., TypeError from network) propagate
}
Check Streaming Not Supported
import { DuelApiError } from "@duel-agents/sdk";
try {
await duel.chat.completions.create({ ...params, stream: true });
} catch (e) {
if (e instanceof DuelApiError && e.status === 400) {
console.log("Streaming not supported yet. Use stream: false.");
}
}