All SDK methods return a response object or throw an error. If Error objects are specified in your OpenAPI Spec, the SDK will throw the appropriate Error type.
Error ObjectStatus CodeContent Type
errors.BadRequest400application/json
errors.Unauthorized401application/json
errors.Forbidden403application/json
errors.NotFound404application/json
errors.TooManyRequests429application/json
errors.InternalServerError500application/json
errors.BadGateway502application/json
errors.ServiceUnavailable503application/json
errors.SDKError4xx-5xx/
Validation errors can also occur when either method arguments or data returned from the server do not match the expected format. The SDKValidationError that is thrown as a result will capture the raw value that failed validation in an attribute called rawValue. Additionally, a pretty() method is available on this error that can be used to log a nicely formatted string since validation errors can list many issues and the plain error string may be difficult read when debugging.
import { Avalanche } from "@avalanche-sdk/chainkit";
import {
  BadGateway,
  BadRequest,
  Forbidden,
  InternalServerError,
  NotFound,
  SDKValidationError,
  ServiceUnavailable,
  TooManyRequests,
  Unauthorized,
} from "@avalanche-sdk/chainkit/models/errors";

const avalancheSDK = new Avalanche({
  apiKey: "<YOUR_API_KEY_HERE>",
  chainId: "43114",
  network: "mainnet",
});

async function run() {
  try {
    await avalancheSDK.data.nfts.reindex({
      address: "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
      tokenId: "145",
    });
  } catch (err) {
    switch (true) {
      case err instanceof SDKValidationError: {
        // Validation errors can be pretty-printed
        console.error(err.pretty());
        // Raw value may also be inspected
        console.error(err.rawValue);
        return;
      }
      case err instanceof BadRequest: {
        // Handle err.data$: BadRequestData
        console.error(err);
        return;
      }
      case err instanceof Unauthorized: {
        // Handle err.data$: UnauthorizedData
        console.error(err);
        return;
      }
      case err instanceof Forbidden: {
        // Handle err.data$: ForbiddenData
        console.error(err);
        return;
      }
      case err instanceof NotFound: {
        // Handle err.data$: NotFoundData
        console.error(err);
        return;
      }
      case err instanceof TooManyRequests: {
        // Handle err.data$: TooManyRequestsData
        console.error(err);
        return;
      }
      case err instanceof InternalServerError: {
        // Handle err.data$: InternalServerErrorData
        console.error(err);
        return;
      }
      case err instanceof BadGateway: {
        // Handle err.data$: BadGatewayData
        console.error(err);
        return;
      }
      case err instanceof ServiceUnavailable: {
        // Handle err.data$: ServiceUnavailableData
        console.error(err);
        return;
      }
      default: {
        throw err;
      }
    }
  }
}

run();