Aptos Names

K
Account

This documentation is using @aptos-labs/ts-sdk v6.0.0

Error Handling

Learn how to handle errors and edge cases when working with the Aptos TS SDK.

Common Error Scenarios

Name Not Found

When fetching a name that doesn't exist or is unclaimed, the SDK returns null:

export async function handleNameNotFound() {
  const name = await aptos.getName({ name: "nonexistent.apt" });

  if (name === null) {
    console.log("Name not found or unclaimed");
    // Handle the case where the name doesn't exist
  }

  return name;
}

No Primary Name

When an address has no primary name set:

export async function handleNoPrimaryName(address: string) {
  const primaryName = await aptos.getPrimaryName({ address });

  if (primaryName === null) {
    console.log("Address has no primary name set");
    // Handle the case where no primary name exists
  }

  return primaryName;
}

No Names for Address

When an address owns no names:

export async function handleNoNamesForAddress(address: string) {
  const result = await aptos.getAccountNames({ accountAddress: address });

  if (result.names.length === 0) {
    console.log("Address owns no names");
    // Handle the case where no names are found
  }

  return result;
}

Network Errors

Handle network-related errors with try-catch:

export async function handleNetworkErrors() {
  try {
    const name = await aptos.getName({ name: "example.apt" });
    // Process the name
    return name;
  } catch (error) {
    if (error instanceof Error) {
      console.error("Error fetching name:", error.message);

      // Handle specific error types
      if (error.message.includes("network")) {
        console.error("Network error occurred");
      } else if (error.message.includes("timeout")) {
        console.error("Request timed out");
      }
    } else {
      console.error("Unknown error:", error);
    }
    throw error;
  }
}

API Key Errors

If you're using an API key and it's invalid or missing:

export async function handleApiKeyErrors() {
  try {
    const name = await aptos.getName({ name: "example.apt" });
    return name;
  } catch (error) {
    if (error instanceof Error && error.message.includes("API key")) {
      console.error("API key error:", error.message);
      // Handle invalid or missing API key
    }
    throw error;
  }
}