Skip to main content

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:

Common Error Scenarios

Thrown when trying to create a customer with an email that already exists.
Thrown when the API key is invalid or missing.
Never log or expose your API key in error messages!
Thrown when the requested resource doesn’t exist.
Caught before the API call when request data is invalid.
Thrown when you’ve exceeded the API rate limit.
The SDK has built-in retry logic with exponential backoff. Most transient errors are automatically retried.

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.