@phala/cloud SDK provides a structured error hierarchy and two error handling patterns: throwing (default) and safe (result-based).
This page covers how to catch and handle errors in the SDK. For the full list of ERR-xxxx error codes (returned by both the API and the CLI), see the Error Codes Reference.
Error Hierarchy
All API errors extendPhalaCloudError:
Catching Errors
Using instanceof
Using Discriminator Properties
Each error class has a boolean discriminator property:Error Classes
PhalaCloudError
Base class for all errors. Properties:| Property | Type | Description |
|---|---|---|
message | string | Error message |
status | number | HTTP status code |
statusText | string | HTTP status text |
detail | string | Record<string, unknown> | Array<{ msg: string }> | Raw error detail from API |
ValidationError
Thrown on HTTP 422. Contains parsed field-level validation errors from FastAPI/Pydantic.| Property | Type | Description |
|---|---|---|
isValidationError | true | Discriminator |
validationErrors | ValidationErrorItem[] | Parsed validation errors |
ValidationErrorItem:
AuthError
Thrown on HTTP 401 or 403.| Property | Type | Description |
|---|---|---|
isAuthError | true | Discriminator |
BusinessError
Thrown on HTTP 400, 409, and other 4xx errors.| Property | Type | Description |
|---|---|---|
isBusinessError | true | Discriminator |
ResourceError
ExtendsBusinessError. Thrown for structured errors with ERR-xxxx codes.
| Property | Type | Description |
|---|---|---|
isResourceError | true | Discriminator |
errorCode | string | Error code (e.g., "ERR-01-001") |
structuredDetails | StructuredErrorDetail[] | Field-level details |
suggestions | string[] | Suggested fixes |
links | ErrorLink[] | Links to documentation |
ServerError
Thrown on HTTP 500+.| Property | Type | Description |
|---|---|---|
isServerError | true | Discriminator |
UnknownError
Thrown for network issues or unexpected errors.| Property | Type | Description |
|---|---|---|
isUnknownError | true | Discriminator |
Safe Methods
Every action has asafe variant that returns SafeResult instead of throwing:
safe<ActionName>:
Formatting Utilities
formatValidationErrors(errors, options?)
Format validation errors for display:
| Option | Type | Default | Description |
|---|---|---|---|
numbered | boolean | true | Number each error |
indent | number | 2 | Indent size in spaces |
showFields | boolean | true | Show field names |
formatErrorMessage(error, options?)
Format any PhalaCloudError for display:
| Option | Type | Default | Description |
|---|---|---|---|
showFields | boolean | true | Show field names in validation errors |
showType | boolean | false | Include error class name |
formatStructuredError(error, options?)
Format ResourceError with codes, details, suggestions, and links:
| Option | Type | Default | Description |
|---|---|---|---|
showErrorCode | boolean | true | Show error code in output |
showSuggestions | boolean | true | Show suggestions |
showLinks | boolean | true | Show documentation links |
Event-Based Error Handling
Listen to errors globally via the client event system:Related
- SDK Overview — getting started
- Error Codes Reference — full list of
ERR-xxxxcodes

