Error Handling
Error Handling for the ChainKit SDK
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 Object | Status Code | Content Type |
|---|---|---|
| errors.BadRequest | 400 | application/json |
| errors.Unauthorized | 401 | application/json |
| errors.Forbidden | 403 | application/json |
| errors.NotFound | 404 | application/json |
| errors.TooManyRequests | 429 | application/json |
| errors.InternalServerError | 500 | application/json |
| errors.BadGateway | 502 | application/json |
| errors.ServiceUnavailable | 503 | application/json |
| errors.SDKError | 4xx-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();Is this guide helpful?