Skip to main content
The Go SDK returns all API errors as *phala.APIError, which implements the error interface. This gives you a single type to handle, with helper methods to classify the error and decide what to do next. This page covers how to catch and handle errors from the SDK. For the full list of ERR-xxxx error codes returned by the API, see the Error Codes Reference.

The APIError Type

Every non-2xx response from the API is wrapped in an *APIError. Use errors.As to extract it from the returned error.

APIError Fields

Error Classification

APIError provides boolean methods to classify errors by category. These make it easy to write switch-style error handling without checking status codes directly.

Classification Methods

A 409 Conflict with a structured ErrorCode is treated as a deterministic business error and is not retryable. Only bare 409 responses (without an error code) are retried automatically.

Structured Errors

Some API errors include a structured ErrorCode (like ERR-01-001) along with detailed suggestions and documentation links. Check for these with IsStructured() or HasErrorCode().

HasErrorCode

Check for a specific error code:

FormatError

The FormatError method produces a human-readable string that includes the error code, message, field-level details, suggestions, and reference links.

Retry-After Header

When the API returns a 429 (Too Many Requests) or 503 (Service Unavailable), it may include a Retry-After header. The RetryAfter() method parses this into a time.Duration.
The method handles both numeric seconds and HTTP date formats. Returns 0 if the header is absent or unparseable.

Automatic Retries

The SDK automatically retries requests that fail with retryable status codes (409, 429, 503). Retries use exponential backoff starting at 1 second with a 20-second cap. Configure the maximum number of retries when creating the client.
Not all methods use automatic retries. Read-only GET requests and non-idempotent operations like CommitCVMProvision do not retry automatically. Lifecycle operations (StartCVM, StopCVM, RestartCVM, etc.) and configuration updates do retry.

Validation Error Details

When IsValidation() is true, the Details field contains field-level error information.

Complete Example

Here is a comprehensive error handling pattern for a CVM provisioning flow: