Error Handling
Handle API errors and exceptions gracefully
Error Handling
The SDK provides a custom DFlowApiError class for handling API errors with detailed information.
DFlowApiError
All API errors are thrown as DFlowApiError instances.
Error Properties
| Property | Type | Description |
|---|---|---|
message | string | Error message |
statusCode | number | HTTP status code |
response | unknown | Raw API response body |
Common Error Codes
| Status | Meaning | Common Causes |
|---|---|---|
400 | Bad Request | Invalid parameters |
401 | Unauthorized | Missing or invalid API key |
404 | Not Found | Market/event doesn't exist |
429 | Rate Limited | Too many requests |
500 | Server Error | API service issue |
Handling Patterns
Basic Error Handling
Rate Limit Handling with Built-in Retry
The SDK provides built-in retry utilities with exponential backoff:
Retry Options:
| Option | Type | Default | Description |
|---|---|---|---|
maxRetries | number | 3 | Maximum retry attempts |
initialDelayMs | number | 1000 | Initial delay before first retry |
maxDelayMs | number | 30000 | Maximum delay between retries |
backoffMultiplier | number | 2 | Multiplier for exponential backoff |
shouldRetry | function | Rate limit + server errors | Custom retry condition |
Creating Reusable Retry Functions
Comprehensive Error Handler
WebSocket Errors
WebSocket errors are handled differently through callbacks:
The WebSocket client automatically attempts to reconnect on disconnection. You can configure this behavior in the client options.
Transaction Errors
For Solana transaction errors:
Pagination Utilities
The SDK provides utilities for handling paginated API responses:
Pagination Options:
| Option | Type | Description |
|---|---|---|
getItems | (response) => items[] | Required. Extracts items array from response. |
getCursor | (response) => cursor | Optional. Extracts cursor (default: response.cursor) |
maxItems | number | Optional. Maximum items to fetch in total |
pageSize | number | Optional. Items per page (API default) |
Best Practices
- Always use try-catch: Wrap API calls in try-catch blocks
- Check error type: Use
instanceof DFlowApiErrorfor API errors - Log appropriately: Include status code and response for debugging
- Use built-in retry: Use
withRetryfor automatic exponential backoff on rate limits - Graceful degradation: Provide fallbacks for non-critical failures