Skip to main content
The Cloudflare SDK provides a comprehensive error hierarchy for handling different failure scenarios.

Error types

When the SDK cannot connect to the API or receives a non-success status code (4xx or 5xx), it throws a subclass of APIError:

Error hierarchy

1

CloudflareError

Base error class for all SDK errors.
2

APIError

Extended error with HTTP status, headers, and response body.
3

Specific Error Types

Status-code-specific error classes for targeted handling.

HTTP status code errors

Connection errors

Connection-related errors inherit from APIConnectionError:

Handling specific errors

Catch and handle different error types:

Accessing error details

The APIError class provides detailed information:

Using catch with promises

Handle errors inline with .catch():

Retry behavior

Certain errors are automatically retried with exponential backoff:
The following errors trigger automatic retries (up to 2 attempts by default):
  • Connection errors (network issues)
  • 408 Request Timeout
  • 409 Conflict
  • 429 Rate Limit
  • ≥500 Internal Server Error
The SDK uses exponential backoff with jitter:
  • Initial retry delay: 0.5 seconds
  • Maximum retry delay: 8 seconds
  • Jitter: Up to 25% variance

Retry headers

The SDK respects retry timing headers:
  • retry-after-ms: Milliseconds to wait before retry
  • retry-after: Seconds to wait or HTTP date
  • x-should-retry: Explicit retry instruction (true/false)

Error handling best practices

1

Always handle APIError

Catch APIError to handle all API-related failures:
2

Check for specific error types

Handle known error conditions explicitly:
3

Log error details

Include status, headers, and error body for debugging:
4

Provide user-friendly messages

Translate API errors into actionable feedback: