import { PolyesterErrorDetail } from "./error-detail.js"; import { RateLimitDetail } from "./rate-limit.schemas.js"; //#region src/shared/errors.d.ts /** * Typed error hierarchy for the Polyester SDK. * * Every error raised by the SDK extends {@link PolyesterError}, which carries a * stable machine-readable `code` and a `retryable` flag. The tree splits on * retryability so callers can triage with a single `instanceof` check: * * ``` * PolyesterError code retryable * ├── TransientError TRANSIENT_FAILURE true * │ ├── NetworkError NETWORK_ERROR true * │ ├── TimeoutError TIMEOUT true * │ ├── RateLimitError RATE_LIMITED true * │ ├── TimestampSkewError TIMESTAMP_SKEW true * │ └── ServiceUnavailableError SERVICE_UNAVAILABLE true * ├── RequestError REQUEST_FAILED false * │ ├── ValidationError VALIDATION_FAILED false * │ │ ├── StaleQuoteError STALE_QUOTE * │ │ └── PolicyScopeMismatchError POLICY_SCOPE_MISMATCH * │ ├── ResourceNotFoundError RESOURCE_NOT_FOUND false * │ ├── NotImplementedError NOT_IMPLEMENTED false * │ ├── AlreadyExistsError ALREADY_EXISTS false * │ ├── PermissionError PERMISSION_DENIED false * │ ├── AuthenticationError UNAUTHENTICATED false * │ ├── PreconditionFailedError PRECONDITION_FAILED false * │ │ ├── RevisionConflictError REVISION_CONFLICT * │ │ ├── PolicyInUseError POLICY_IN_USE * │ │ ├── PolicyLockedError POLICY_LOCKED * │ │ ├── SubaccountChallengeInvalidError SUBACCOUNT_CHALLENGE_INVALID * │ │ └── MfaLastFactorRequiredError MFA_LAST_FACTOR_REQUIRED * │ ├── ConfigurationError INVALID_CONFIGURATION false * │ ├── MfaRequiredError MFA_REQUIRED false * │ │ ├── MfaEnrollmentRequiredError MFA_ENROLLMENT_REQUIRED * │ │ ├── StepUpRequiredError STEP_UP_REQUIRED * │ │ └── SessionElevationRequiredError SESSION_ELEVATION_REQUIRED * │ └── MfaVerificationError MFA_VERIFICATION_FAILED false * ├── InternalServerError INTERNAL_SERVER_ERROR false * └── RealtimeError REALTIME_ERROR varies * ``` * * Catalog errors (`CatalogLookupError`, `CatalogConversionError`, …) plug into * the same tree under `RequestError`/`ValidationError`. * * Errors mapped from RPC failures keep the original `ConnectError` as `cause`. * * @example * ```ts * try { * await client.orders.createSpotOrder(input); * } catch (err) { * if (err instanceof RateLimitError) await sleep(err.retryAfterMs ?? 1000); * else if (err instanceof TransientError) retry(); * else if (err instanceof StepUpRequiredError) promptForMfa(); * else throw err; * } * ``` */ /** Stable machine-readable codes carried by built-in SDK errors. */ type PolyesterErrorCode = "TRANSIENT_FAILURE" | "NETWORK_ERROR" | "TIMEOUT" | "RATE_LIMITED" | "TIMESTAMP_SKEW" | "SERVICE_UNAVAILABLE" | "REQUEST_FAILED" | "VALIDATION_FAILED" | "STALE_QUOTE" | "RESOURCE_NOT_FOUND" | "NOT_IMPLEMENTED" | "ALREADY_EXISTS" | "PERMISSION_DENIED" | "UNAUTHENTICATED" | "PRECONDITION_FAILED" | "REVISION_CONFLICT" | "POLICY_IN_USE" | "POLICY_LOCKED" | "POLICY_SCOPE_MISMATCH" | "INVALID_CONFIGURATION" | "MFA_REQUIRED" | "MFA_ENROLLMENT_REQUIRED" | "STEP_UP_REQUIRED" | "SESSION_ELEVATION_REQUIRED" | "MFA_LAST_FACTOR_REQUIRED" | "MFA_VERIFICATION_FAILED" | "INTERNAL_SERVER_ERROR" | "REALTIME_ERROR" | "CATALOG_LOOKUP_MISS" | "CATALOG_NOT_READY" | "CATALOG_CONVERSION_INVALID" | "CATALOG_VALIDATION_FAILED"; interface PolyesterErrorOptions { /** Underlying error, typically the original `ConnectError` for RPC failures. */ cause?: unknown; /** Recognized structured backend rejection, decoded at the RPC boundary. */ detail?: PolyesterErrorDetail; } declare function normalizeErrorMessage(message: string): string; /** * Base class for every error raised by the Polyester SDK. * * Check `retryable` (or `instanceof TransientError`) to decide whether the * same call can be safely retried, and switch on `code` for exhaustive, * minification-safe handling. */ declare abstract class PolyesterError extends Error { /** Stable machine-readable error code, e.g. `"RATE_LIMITED"`. */ abstract readonly code: string; /** Whether retrying the same operation may succeed. */ abstract readonly retryable: boolean; readonly detail: PolyesterErrorDetail | undefined; constructor(message: string, options?: PolyesterErrorOptions); } /** * A transient failure — the request may never have reached the backend or the * backend was temporarily unable to serve it. Follow the operation's retry * contract: reuse requestId for idempotent mutations. For single-order creation, * reconcile by clientOrderId; reusing it returns a conflict rather than replay. */ declare class TransientError extends PolyesterError { readonly code: string; readonly retryable = true; constructor(message: string, options?: PolyesterErrorOptions); } /** The request could not be sent or the connection failed mid-flight. */ declare class NetworkError extends TransientError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The request timed out before the backend produced a response. */ declare class TimeoutError extends TransientError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } interface RateLimitErrorOptions extends PolyesterErrorOptions { /** Suggested wait before retrying, when the backend provided one. */ retryAfterMs?: number; /** Structured quota state attached to the rejection, when provided. */ rateLimit?: RateLimitDetail; } /** The backend rejected the request due to rate limiting. */ declare class RateLimitError extends TransientError { readonly code: string; /** Suggested wait before retrying, in milliseconds, when known. */ readonly retryAfterMs?: number; /** Structured quota state attached to the rejection, when provided. */ readonly rateLimit?: RateLimitDetail; constructor(message: string, options?: RateLimitErrorOptions); } /** * The API-key signature timestamp fell outside the backend's skew window, e.g. * because the request waited too long in the runtime's fetch queue during a * large concurrent burst, or the local clock drifted. Retrying the call signs * it again with a fresh timestamp; bound concurrency if it recurs. */ declare class TimestampSkewError extends TransientError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The backend is temporarily unavailable (e.g. overloaded or restarting). */ declare class ServiceUnavailableError extends TransientError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** * The request itself was rejected — retrying the identical call will fail the * same way. Fix the input, authentication, or permissions first. */ declare class RequestError extends PolyesterError { readonly code: string; readonly retryable = false; constructor(message: string, options?: PolyesterErrorOptions); } /** The request input failed validation (client- or server-side). */ declare class ValidationError extends RequestError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The submitted client reference quote exceeded the market's configured drift limit. */ declare class StaleQuoteError extends ValidationError { readonly code: "STALE_QUOTE"; constructor(message: string, options?: PolyesterErrorOptions); } /** The requested resource does not exist (or is not visible to the caller). */ declare class ResourceNotFoundError extends RequestError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The backend does not implement the requested operation (HTTP 501 / Connect `unimplemented`). */ declare class NotImplementedError extends RequestError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The resource already exists (e.g. a duplicate `clientOrderId`). */ declare class AlreadyExistsError extends RequestError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The caller is authenticated but not allowed to perform this operation. */ declare class PermissionError extends RequestError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The caller is not authenticated (missing, expired, or invalid credentials). */ declare class AuthenticationError extends RequestError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The system is not in a state that allows this operation (e.g. insufficient balance). */ declare class PreconditionFailedError extends RequestError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** * The resource changed after it was read, so the submitted expected revision * is stale. Refetch and let the caller reconcile; do not blindly retry. */ declare class RevisionConflictError extends PreconditionFailedError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** * The subaccount authorization is expired, replaced by a newer challenge, replayed, * or otherwise invalid. Request a fresh challenge via `subaccounts.createChallenge`. */ declare class SubaccountChallengeInvalidError extends PreconditionFailedError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The policy cannot be removed while accounts or API keys still use it. */ declare class PolicyInUseError extends PreconditionFailedError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The policy is locked against the requested mutation. */ declare class PolicyLockedError extends PreconditionFailedError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The policy does not belong to the account scope targeted by the request. */ declare class PolicyScopeMismatchError extends ValidationError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The SDK was configured incorrectly (bad environment, missing credentials, …). */ declare class ConfigurationError extends RequestError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** * The operation requires additional multi-factor authentication. Catch this * class to handle all MFA flows, or the subclasses to branch per flow. */ declare class MfaRequiredError extends RequestError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The user must enroll in MFA before this operation can proceed. */ declare class MfaEnrollmentRequiredError extends MfaRequiredError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The operation requires a fresh MFA step-up proof (retry with `X-Auth-Step-Up`). */ declare class StepUpRequiredError extends MfaRequiredError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The operation requires a recent MFA-elevated session, not a one-use step-up proof. */ declare class SessionElevationRequiredError extends MfaRequiredError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The requested deletion would leave the account without an active MFA factor. */ declare class MfaLastFactorRequiredError extends PreconditionFailedError { readonly code: string; constructor(message: string, options?: PolyesterErrorOptions); } /** The reason an MFA challenge could not be completed. */ type MfaVerificationFailureReason = "challenge-invalid" | "challenge-locked" | "otp-invalid" | "recovery-code-invalid" | "passkey-invalid"; /** * An expected MFA verification rejection. Callers should select their own * user-facing copy from `reason` instead of displaying the backend message. */ declare class MfaVerificationError extends RequestError { readonly reason: MfaVerificationFailureReason; readonly code: string; constructor(message: string, reason: MfaVerificationFailureReason, options?: PolyesterErrorOptions); } /** * The backend failed unexpectedly or returned a malformed response. Not * automatically retryable — the failure may recur and mutations may have * partially applied. */ declare class InternalServerError extends PolyesterError { readonly code: string; readonly retryable = false; constructor(message: string, options?: PolyesterErrorOptions); } interface RealtimeErrorOptions extends PolyesterErrorOptions { /** Centrifugo protocol, disconnect, or unsubscribe code; 0 when none applies. */ realtimeCode?: number; /** True when the realtime client is already retrying on its own. */ retryable?: boolean; } /** * A realtime subscription failure reported by the server or the websocket * client. `retryable` errors (e.g. code 109, token expired) are being retried * automatically; only non-retryable ones end the subscription. */ declare class RealtimeError extends PolyesterError { readonly code: "REALTIME_ERROR"; readonly retryable: boolean; readonly realtimeCode: number; constructor(message: string, options?: RealtimeErrorOptions); } /** * Checks whether an error represents an aborted request (caller-initiated via * `AbortSignal`). Aborts are intentionally NOT part of the {@link PolyesterError} * tree and are never retryable. */ declare function isAbortError(err: unknown): boolean; /** Maps a plain HTTP status code onto the SDK error tree. */ declare function errorFromHttpStatus(status: number, message: string, options?: RateLimitErrorOptions): PolyesterError; //#endregion export { AlreadyExistsError, AuthenticationError, ConfigurationError, InternalServerError, MfaEnrollmentRequiredError, MfaLastFactorRequiredError, MfaRequiredError, MfaVerificationError, MfaVerificationFailureReason, NetworkError, NotImplementedError, PermissionError, PolicyInUseError, PolicyLockedError, PolicyScopeMismatchError, PolyesterError, PolyesterErrorCode, type PolyesterErrorDetail, PolyesterErrorOptions, PreconditionFailedError, RateLimitError, RateLimitErrorOptions, RealtimeError, RealtimeErrorOptions, RequestError, ResourceNotFoundError, RevisionConflictError, ServiceUnavailableError, SessionElevationRequiredError, StaleQuoteError, StepUpRequiredError, SubaccountChallengeInvalidError, TimeoutError, TimestampSkewError, TransientError, ValidationError, errorFromHttpStatus, isAbortError, normalizeErrorMessage }; //# sourceMappingURL=errors.d.ts.map