The Python SDK provides a structured error hierarchy and two error handling patterns: exceptions (default) and safe results. Every method that can fail has aDocumentation Index
Fetch the complete documentation index at: https://docs.phala.com/llms.txt
Use this file to discover all available pages before exploring further.
safe_* variant that returns a result object instead of raising.
This page covers how to catch and handle errors in the SDK. For the full list of ERR-xxxx error codes returned by the API, see the Error Codes Reference.
Error Hierarchy
All SDK exceptions inherit fromPhalaCloudError:
Catching Errors
Basic Exception Handling
Order matters when catching exceptions. Catch more specific subclasses first —
ResourceError before BusinessError, and BusinessError before ApiError.Error Classes
PhalaCloudError
Base class for all SDK exceptions. Catches everything.RequestError
Raised when the HTTP request itself fails (DNS resolution, connection timeout, TLS errors). This wrapshttpx.HTTPError.
ApiError
Base class for all HTTP error responses from the API.| Property | Type | Description |
|---|---|---|
status_code | int | HTTP status code |
code | str | None | Error code string from response |
detail | Any | Raw error detail from API response |
AuthError
Raised on HTTP 401 or 403. Usually means the API key is invalid or expired.BusinessError
Raised on HTTP 400, 409, and other 4xx client errors that are not auth-related.ConflictError
Subclass ofBusinessError. Raised on HTTP 409 when there is no structured error code. These conflicts are typically transient — for example, another operation is already in progress on the same CVM. Retrying after a short delay often resolves the issue.
ResourceError
Subclass ofBusinessError. Raised when the API returns a structured error with an error_code field. These are deterministic business errors with additional context.
| Property | Type | Description |
|---|---|---|
error_code | str | None | Structured code (e.g., "ERR-01-001") |
structured_details | list[dict] | None | Field-level error details |
suggestions | list[str] | None | Suggested fixes |
links | list[dict] | None | Links to documentation |
ServerError
Raised on HTTP 500+. Indicates a problem on the Phala Cloud side.ValidationError
Raised when a response passes HTTP validation but fails Pydantic model validation. This is rare and usually indicates an API response format change.Safe Methods
Every action method has asafe_* counterpart that wraps the result in a SafeResult dataclass instead of raising exceptions.
SafeResult
unwrap() method gives you a quick escape hatch when you want safe handling in most cases but still want to raise in unexpected situations:
PhalaCloudError and Pydantic ValidationError. Other exceptions (like KeyboardInterrupt) still propagate normally.
Async Safe Methods
The async client works identically:Error Codes
Thephala_cloud.error_codes module provides constants for all structured error codes. Use these to match specific errors in your error handling logic.
Available Error Code Modules
| Module | Prefix | Category |
|---|---|---|
| 01 | ERR-01-xxx | CVM preflight and compose hash |
| 02 | ERR-02-xxx | Inventory and resource allocation |
| 03 | ERR-03-xxx | CVM operations |
| 04 | ERR-04-xxx | Workspace and billing |
| 05 | ERR-05-xxx | Credentials and tokens |
| 06 | ERR-06-xxx | Authentication — returned as OAuth redirect responses, not JSON API errors |
Practical Patterns
Retry on Conflict
ConflictError (409 without structured code) is often transient. A simple retry loop handles it:
Combining Safe + Exception Handling
Use safe methods for expected failures and exceptions for unexpected ones:Related
- SDK Overview — getting started
- Error Codes Reference — full list of
ERR-xxxxcodes

