Skip to main content
The @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 extend PhalaCloudError:

Catching Errors

Using instanceof

Using Discriminator Properties

Each error class has a boolean discriminator property:

Error Classes

PhalaCloudError

Base class for all errors. Properties:

ValidationError

Thrown on HTTP 422. Contains parsed field-level validation errors from FastAPI/Pydantic. Each ValidationErrorItem:

AuthError

Thrown on HTTP 401 or 403.

BusinessError

Thrown on HTTP 400, 409, and other 4xx errors.

ResourceError

Extends BusinessError. Thrown for structured errors with ERR-xxxx codes.

ServerError

Thrown on HTTP 500+.

UnknownError

Thrown for network issues or unexpected errors.

Safe Methods

Every action has a safe variant that returns SafeResult instead of throwing:
All safe methods follow the naming convention safe<ActionName>:

Formatting Utilities

formatValidationErrors(errors, options?)

Format validation errors for display:
Options:

formatErrorMessage(error, options?)

Format any PhalaCloudError for display:
Options:

formatStructuredError(error, options?)

Format ResourceError with codes, details, suggestions, and links:
Options:

Event-Based Error Handling

Listen to errors globally via the client event system: