Skip to content
Docs

Cloud SDK

Error handling

Every @gray3/cloud failure is a GrayError with a stable code, HTTP status, and retryable flag.

GrayError

Every failed call throws a GrayError. It has the same fields as the API error envelope plus the HTTP status:

FieldRequiredDescription
codeYesStable machine code, such as credits_exhausted. Branch on this.
messageYesHuman-readable text. Log it; do not parse it.
retryableYesTrue when retrying with backoff may succeed.
httpStatusYesHTTP status, such as 402 or 429.

Network failures and non-JSON responses surface as provider_unavailable with retryable: true.

Handling errors

ts
import { isGrayError } from "@gray3/cloud";

try {
  await gray.portfolio.getMany({ address, chainIds });
} catch (err) {
  if (!isGrayError(err)) throw err;

  switch (err.code) {
    case "credits_exhausted":   // 402
      return showUpgradePrompt();
    case "plan_feature_denied": // 403
    case "plan_chain_denied":   // 403
      return showPlanLimit();
    case "rate_limit_exceeded": // 429
      return retryLater();
    case "unsupported_capability":
      return hideFeatureForChain();
    default:
      if (err.retryable) return retryWithBackoff();
      throw err;
  }
}

The full list of codes is in the Error codes catalog.