Principle:2aronS Duel Agents Typed Error Classification
| Knowledge Sources | |
|---|---|
| Domains | Error_Handling, API_Design |
| Last Updated | 2026-10-27 14:00 GMT |
Overview
Pattern for classifying HTTP failures into a typed error hierarchy where authentication failures and general API failures are distinct subclasses of a common base, enabling callers to catch the failure category they can recover from without parsing status codes.
Description
When a client makes HTTP requests, failures fall into qualitatively different categories that callers handle differently:
- Authentication errors (HTTP 401/403) — the key is rejected; the user must re-enter or renew it. This is often recoverable by prompting for a new key.
- API errors (other non-2xx) — the server understood the request but returned an error (rate limit, bad request, server error). The response body may carry structured error details useful for debugging.
A flat "throw Error with a message" approach forces callers to parse the message or inspect a status field to distinguish these cases. A typed error hierarchy makes the distinction structural:
- A base `DuelError` class serves as the catch-all for SDK-specific failures
- `DuelAuthError` (extends `DuelError`) carries a `status` (default 401) and is thrown only for 401/403
- `DuelApiError` (extends `DuelError`) carries both `status` and the parsed `body`, thrown for all other non-2xx responses
Callers can now write `catch (e) { if (e instanceof DuelAuthError) { ... } }` without inspecting messages or status fields. The base class `DuelError` allows a catch-all that filters out non-SDK errors (e.g., network failures remain native `TypeError`s).
Usage
Use this principle when:
- Building a client library where callers need to distinguish error categories programmatically
- Different error categories require different user-facing remediation (re-enter key vs. retry later)
- You want to attach structured data (status, response body) to errors without losing the ability to `instanceof`-check
- You need a base class that callers can catch to filter only SDK-originated errors
Theoretical Basis
The hierarchy is a single-inheritance tree rooted at a base error:
# Abstract algorithm (NOT real implementation)
class DuelError(Exception):
"""Base for all SDK errors."""
def __init__(self, message):
super().__init__(message)
self.name = "DuelError"
class DuelAuthError(DuelError):
"""401/403: key rejected by server."""
def __init__(self, message, status=401):
super().__init__(message)
self.name = "DuelAuthError"
self.status = status
class DuelApiError(DuelError):
"""Other non-2xx: carries status and parsed body."""
def __init__(self, message, status, body):
super().__init__(message)
self.name = "DuelApiError"
self.status = status
self.body = body
The classification logic at the response site:
if res.status in (401, 403):
raise DuelAuthError(f"Rejected ({res.status})", res.status)
if not res.ok:
raise DuelApiError(f"API error {res.status}", res.status, parsed_body)
Callers catch by category:
try:
result = client.chat.completions.create(params)
except DuelAuthError:
prompt_for_new_key() # recoverable: re-authenticate
except DuelApiError as e:
log_error(e.status, e.body) # inspect structured body
retry_or_report(e)
except DuelError:
handle_unknown_sdk_error()