TypeScript SDK — Error Handling
The SDK uses typed error classes so you can catch specific failure modes. Every SDK error extends SdkError.
Error class hierarchy
Code
Catching errors
Catch broadly or narrowly depending on your needs:
Code
Error metadata
Every ApiError carries structured fields for programmatic handling:
Code
The code and category fields are stable across SDK versions — use them for programmatic branching. The reason field is the verbatim server string and may change.
Retries
The SDK retries automatically for generated safe-read operations only (GET-equivalent endpoints). Retries trigger on:
- Network failures
- HTTP 429 (rate limit) — respects
Retry-Afterheader - HTTP 502, 503, 504 (transient server errors)
Mutating operations are never retried. A failed order placement stays failed — you decide whether to retry.
Configure retry behavior:
Code
Safe error serialization
Use serializeError() to log errors safely. It strips raw response bodies and credentials while preserving structure:
Code
The serialized output includes: name, message (redacted), status, reason (only recognized values), code, category, metadata (endpoint, method, correlation ID), and operationContext.
Diagnostics
The SDK emits structured diagnostic events across REST, OAuth, WebSocket, and order-book operations. Diagnostics are silent by default.
Diagnostic listener
Receive every event as a structured object:
Code
When using OAuthAuth, pass the same onDiagnostic callback to include token exchange and refresh events:
Code
OpenTelemetry (optional)
The SDK includes an optional adapter at @gemini-markets/sdk/opentelemetry. It
maps safe diagnostics to OpenTelemetry client spans for REST and OAuth
requests, WebSocket requests and subscriptions, reconnects, and order-book
recovery events. The adapter does not configure a provider or exporter, and
the core SDK remains dependency-free unless this subpath is imported.
Install the OpenTelemetry API alongside the provider and exporter selected by your application:
Code
Pass the tracer from your configured provider to the adapter and connect its diagnostic listener to the client:
Code
Spans use SpanKind.CLIENT, standard HTTP method and response-status
attributes where applicable, and Gemini-specific attributes for operation
names, correlation IDs, retry counts, exchange request IDs, stream names, and
symbols. Request and response bodies, credentials, tokens, signatures, and
raw error bodies are not copied into spans. Configure and flush the provider
through your application’s normal OpenTelemetry shutdown path.
By default, HTTP span names use the method and stable generated operation name
when available (for example, POST trading.createNewOrder); otherwise they
use only the method. Dynamic endpoint values are not used as span names. This
adapter provides SDK-level logical spans; use your OpenTelemetry HTTP or
undici instrumentation for wire-level HTTP context propagation, and avoid
enabling two instrumentations for the same network span unless both layers are
intentional.
Console logger
For development, use the built-in console logger:
Code
Log levels: debug, info, warn, error. Set minLevel to control verbosity.
What's redacted
Diagnostic events and serialized errors never include: request bodies, response bodies, credentials, signatures, tokens, API keys, or nonces. They do include: endpoint paths, HTTP methods, status codes, correlation IDs, exchange request IDs, rate-limit headers, retry counts, and content types.
Use serializeError(err, { includeRawBody: true }) only when you need the raw body for debugging, and treat that output as sensitive.
What's next
- Patterns & recipes — timeouts, cancellation, pagination, and heartbeat
- API Reference — all operations with their retry and validation behavior
- WebSocket Sessions — reconnection and connection error recovery
- Transport & Signing — retry policy internals