Error Handling
The SDK throws structured error objects with error codes, suggestions, and documentation links. Every error extendsZendFiError and can be caught with standard try/catch.
Error Hierarchy
Error Properties
EveryZendFiError includes:
| Property | Type | Description |
|---|---|---|
code | string | Machine-readable error code |
type | string | Error category |
message | string | Human-readable description |
suggestion | string? | Actionable fix recommendation |
docs_url | string | Link to error documentation |
statusCode | number? | HTTP status code (0 for network errors) |
response | unknown | Raw API response body |
Error Types
AuthenticationError
Thrown when the API key is invalid, expired, or revoked.PaymentError
Thrown for payment-specific issues like insufficient balance or declined transactions.ValidationError
Thrown when request parameters are invalid.NetworkError
Thrown for connection failures, timeouts, and server errors.RateLimitError
Thrown when you exceed the API rate limits.WebhookError
Thrown during webhook signature verification failures.Handling Patterns
Catch Specific Errors
Type Guard
UseisZendFiError() to check if any error is a ZendFi error:
Error Display
ThetoString() method formats errors with suggestions and docs links:
Error Codes Reference
| Code | Type | Description |
|---|---|---|
invalid_api_key | Authentication | API key not recognized |
api_key_expired | Authentication | API key has expired |
api_key_revoked | Authentication | API key was revoked |
insufficient_balance | Payment | Wallet balance too low |
payment_declined | Payment | Payment was rejected |
payment_expired | Payment | Payment window elapsed |
invalid_amount | Validation | Amount is invalid |
invalid_currency | Validation | Currency not supported |
missing_required_field | Validation | Required field not provided |
invalid_parameter | Validation | Parameter value invalid |
network_error | Network | Connection failed |
timeout | Network | Request timed out |
rate_limit_exceeded | Rate Limit | Too many requests |
webhook_signature_invalid | Webhook | HMAC signature mismatch |
webhook_timestamp_too_old | Webhook | Signature expired (>5 min) |
Automatic Retries
The SDK retries automatically for transient errors:- 5xx server errors: Retried up to
config.retriestimes - Network failures: Retried with exponential backoff
- 4xx client errors: Never retried
Utility Exports
createZendFiError()
Factory function that creates the appropriate error subclass based on HTTP status code:
| Status Code | Error Class |
|---|---|
| 401 | AuthenticationError |
| 429 | RateLimitError |
| 400, 422 | ValidationError |
| 402 | PaymentError |
| 0, 500+ | NetworkError |
| Other | ApiError |