メインコンテンツへスキップ

エラーフォーマット

すべてのAgentWallex APIエラーは一貫したJSON形式に従います:
{
  "code": "invalid_request",
  "type": "invalid_request_error",
  "message": "human readable error description"
}
フィールド説明
code機械可読エラーコード(例:policy_violationinsufficient_funds
typeエラーカテゴリ(下表参照)
message何が問題だったかの人間が読める説明

エラータイプ

タイプHTTPステータス説明
invalid_request_error400リクエストボディまたはパラメータが無効です
authentication_error401APIキー/トークンが不足または無効です
authorization_error403有効な資格情報ですが権限が不足しています
not_found_error404リクエストされたリソースが存在しません
rate_limit_error429リクエストが多すぎます — 指定された遅延後にリトライしてください
internal_error500サーバーサイドエラー — 持続する場合はサポートに連絡してください

一般的なエラーコード

コードタイプ説明
policy_violationinvalid_request_errorトランザクションがポリシーエンジンによってブロックされました
insufficient_fundsinvalid_request_errorウォレット残高がトランザクションに対して不足しています
agent_frozeninvalid_request_errorエージェントが凍結されています — トランザクション前に凍結解除してください
invalid_chaininvalid_request_errorサポートされていないまたは無効なチェーンIDです
invalid_tokeninvalid_request_error指定されたチェーンでサポートされていないトークンです
invalid_addressinvalid_request_error受取人アドレスの形式が不正です
session_expiredinvalid_request_errorx402セッションの有効期限が切れたか予算を超過しました
rate_limit_exceededrate_limit_errorAPIレート制限に達しました
approval_timeoutinvalid_request_errorタイムアウト内に人間承認が得られませんでした

SDKエラーハンドリング

TypeScript

TypeScript SDKは特定のエラータイプに対する型付きエラークラスを提供します:
import {
  AgentWallexError,
  PolicyViolationError,
} from "@agentwallex/sdk";

try {
  const tx = await aw.payments.send({
    agentId: "agent_abc123",
    to: "0xRecipientAddress",
    amount: "50.00",
    token: "USDC",
  });
} catch (error) {
  if (error instanceof PolicyViolationError) {
    console.log(`Policy violated: ${error.rule}`);
    console.log(`Details: ${error.message}`);
    // Handle: adjust amount, use different address, etc.
  } else if (error instanceof AgentWallexError) {
    console.log(`API error [${error.code}]: ${error.message}`);

    switch (error.code) {
      case "insufficient_funds":
        // Top up the wallet
        break;
      case "agent_frozen":
        // Unfreeze or use a different agent
        break;
      case "rate_limit_exceeded":
        // Wait and retry
        break;
      default:
        // Log and alert
        break;
    }
  } else {
    // Network error or unexpected exception
    console.error("Unexpected error:", error);
  }
}

Python

from agentwallex import AgentWallexError, PolicyViolationError

try:
    tx = await aw.payments.send(
        agent_id="agent_abc123",
        to="0xRecipientAddress",
        amount="50.00",
        token="USDC",
    )
except PolicyViolationError as e:
    print(f"Policy violated: {e.rule}")
    print(f"Details: {e.message}")
except AgentWallexError as e:
    print(f"API error [{e.code}]: {e.message}")
except Exception as e:
    print(f"Unexpected error: {e}")

リトライ戦略

リトライには指数バックオフを使用してください。rate_limit_errorinternal_errorのみリトライしてください。invalid_request_errorauthentication_errorはリトライせず、リクエストの修正が必要です。
async function withRetry<T>(
  fn: () => Promise<T>,
  maxRetries: number = 3
): Promise<T> {
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
    try {
      return await fn();
    } catch (error) {
      if (
        error instanceof AgentWallexError &&
        (error.type === "rate_limit_error" || error.type === "internal_error") &&
        attempt < maxRetries
      ) {
        const delay = Math.pow(2, attempt) * 1000; // 1s, 2s, 4s
        await new Promise(resolve => setTimeout(resolve, delay));
        continue;
      }
      throw error;
    }
  }
  throw new Error("Max retries exceeded");
}

// Usage
const tx = await withRetry(() =>
  aw.payments.send({
    agentId: "agent_abc123",
    to: "0xRecipientAddress",
    amount: "50.00",
    token: "USDC",
  })
);

HTTPステータスコードリファレンス

ステータス意味アクション
200成功レスポンスを処理します
201作成完了リソースが正常に作成されました
400不正なリクエストリクエストパラメータを修正してください
401未認証APIキーを確認してください
403禁止権限/プランティアを確認してください
404未検出リソースIDが存在するか確認してください
429レート制限バックオフで待機してリトライしてください
500サーバーエラーバックオフでリトライ、持続する場合はサポートに連絡してください