Skip to main content

Error Handling

The SDK throws structured error objects with error codes, suggestions, and documentation links. Every error extends ZendFiError and can be caught with standard try/catch.

Error Hierarchy

Error Properties

Every ZendFiError includes:

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

Use isZendFiError() to check if any error is a ZendFi error:

Error Display

The toString() method formats errors with suggestions and docs links:

Error Codes Reference

Automatic Retries

The SDK retries automatically for transient errors:
  • 5xx server errors: Retried up to config.retries times
  • Network failures: Retried with exponential backoff
  • 4xx client errors: Never retried
The backoff formula is 2n×10002^n \times 1000 ms, where nn is the attempt number.

Utility Exports

createZendFiError()

Factory function that creates the appropriate error subclass based on HTTP status code:
Mapping:

ERROR_CODES

Const object mapping all SDK error code strings:

Types