Error Types
The SDK exports two main error classes:AlignError
Thrown when the API returns an error response (4xx or 5xx status codes).AlignValidationError
Thrown when request data fails local validation before being sent to the API.Basic Error Handling
Always wrap API calls in try-catch blocks:- TypeScript
- JavaScript
Common Error Scenarios
Duplicate Email (409 Conflict)
Duplicate Email (409 Conflict)
Thrown when trying to create a customer with an email that already exists.
Invalid API Key (401 Unauthorized)
Invalid API Key (401 Unauthorized)
Resource Not Found (404)
Resource Not Found (404)
Thrown when the requested resource doesn’t exist.
Validation Errors
Validation Errors
Caught before the API call when request data is invalid.
Rate Limiting (429 Too Many Requests)
Rate Limiting (429 Too Many Requests)
Thrown when you’ve exceeded the API rate limit.
Error Handling Patterns
Centralized Error Handler
Create a reusable error handler for consistent error management:Retry with Backoff
For critical operations, implement custom retry logic:Logging Errors
Enable SDK logging for debugging:The SDK uses pino for logging. Set
LOG_LEVEL environment variable to control log verbosity.Best Practices
Always Use Try-Catch
Wrap all API calls in try-catch blocks to prevent unhandled promise
rejections.
Check Error Types
Use
instanceof to distinguish between validation errors and API errors.Log Appropriately
Log errors for debugging but never log sensitive data like API keys.
User-Friendly Messages
Transform technical errors into user-friendly messages for your UI.