Skip to content

Chat Errors

Defined in: chat/errors.ts:33

Unified error class for all chat SDK errors

new ChatError(message, options): ChatError

Defined in: chat/errors.ts:39

string

ChatErrorOptions

ChatError

AgentSDKError.constructor

readonly code: ErrorCode

Defined in: chat/errors.ts:34

Machine-readable error code. Prefer values from the ErrorCode enum.

AgentSDKError.code

readonly optional httpStatus: number

Defined in: errors.ts:25

HTTP status code hint for error classification

AgentSDKError.httpStatus

readonly retryable: boolean

Defined in: chat/errors.ts:35

Whether this error is safe to retry

AgentSDKError.retryable

readonly optional retryAfter: number

Defined in: chat/errors.ts:36

readonly timestamp: string

Defined in: chat/errors.ts:37

static is(error): error is AgentSDKError

Defined in: errors.ts:36

Check if an error is an AgentSDKError (works across bundled copies)

unknown

error is AgentSDKError

AgentSDKError.is


Defined in: chat/errors.ts:253

Exponential backoff with optional jitter

new ExponentialBackoffStrategy(options?): ExponentialBackoffStrategy

Defined in: chat/errors.ts:259

ExponentialBackoffOptions

ExponentialBackoffStrategy

nextDelay(attempt, error): number | null

Defined in: chat/errors.ts:266

Return delay in ms for the given attempt (0-based), or null to stop

number

ChatError

number | null

RetryStrategy.nextDelay

Defined in: chat/errors.ts:19

Options for constructing a ChatError

optional cause: unknown

Defined in: chat/errors.ts:27

Original cause, if wrapping another error

code: ErrorCode

Defined in: chat/errors.ts:21

Machine-readable error code

optional retryable: boolean

Defined in: chat/errors.ts:23

Whether this error is retryable (default: false)

optional retryAfter: number

Defined in: chat/errors.ts:25

Retry delay hint in milliseconds


Defined in: chat/errors.ts:241

Options for ExponentialBackoffStrategy

optional baseMs: number

Defined in: chat/errors.ts:243

Base delay in ms (default: 1000)

optional jitter: number

Defined in: chat/errors.ts:249

Jitter factor 0–1 (default: 0.1)

optional maxAttempts: number

Defined in: chat/errors.ts:247

Maximum number of attempts (default: 3)

optional maxMs: number

Defined in: chat/errors.ts:245

Maximum delay in ms (default: 30000)


Defined in: chat/errors.ts:284

Options for withRetry execution

optional onRetry: (error, attempt, delayMs) => void

Defined in: chat/errors.ts:288

Called before each retry with the error and delay

ChatError

number

number

void

optional signal: AbortSignal

Defined in: chat/errors.ts:286

Abort signal to cancel retries


Defined in: chat/errors.ts:235

Strategy for computing retry delays

nextDelay(attempt, error): number | null

Defined in: chat/errors.ts:237

Return delay in ms for the given attempt (0-based), or null to stop

number

ChatError

number | null

classifyError(error): ChatError

Defined in: chat/errors.ts:69

Classify an unknown thrown value into a ChatError with the appropriate code. Pattern-matches against common error shapes:

  • Already a ChatError → returned as-is
  • Fetch/network errors (ECONNREFUSED, ETIMEDOUT, etc.)
  • HTTP status codes (401→AUTH_INVALID, 429→RATE_LIMIT, 5xx→PROVIDER_ERROR)
  • Timeout patterns
  • Zod validation errors
  • Context overflow patterns
  • Unknown → wrapped as ChatError with PROVIDER_ERROR

unknown

The thrown value to classify

ChatError

ChatError with appropriate error code and retryable flag


isRetryable(error): boolean

Defined in: chat/errors.ts:337

Type guard: check if an error is retryable

unknown

The error to check

boolean

True if error is a retryable ChatError


withRetry<T>(fn, strategy, options?): Promise<T>

Defined in: chat/errors.ts:302

Execute an async function with automatic retries using the provided strategy. Respects ChatError.retryable and ChatError.retryAfter. Classifies non-ChatError errors before deciding on retry.

T

() => Promise<T>

Async function to execute

RetryStrategy

Retry strategy providing delay calculations

RetryOptions

Optional abort signal and retry callback

Promise<T>

Result of fn on success

ChatError when all retries exhausted or error is non-retryable

Re-exports ErrorCode