Skip to content

Latest commit

 

History

History
151 lines (142 loc) · 4.33 KB

error-handling.md

File metadata and controls

151 lines (142 loc) · 4.33 KB

Error Handling

This guide shows you how to handle errors originating from the SDK. SDK by default exports the following error classes from file errors.ts

Error Hierarchy

BaseError
├── ArgumentError
├── IllegalArgumentError
├── ArgumentCountMismatchError
├── InsufficientBalanceError
├── MissingParamError
├── NoSerializerFoundError
├── RequestTimedOutError
├── TxTimedOutError
├── TypeError
├── UnsupportedPlatformError
├── UnsupportedProtocolError
├── NotImplementedError
├── UnsupportedVersionError
├── LogicError
│
├── InternalError
│   └── UnexpectedTsError
│
├── AccountError
│   └── UnavailableAccountError
│
├── AensError
│   ├── AensPointerContextError
│   ├── InsufficientNameFeeError
│   └── InvalidAensNameError
│
├── AeppError
│   ├── InvalidRpcMessageError
│   ├── MissingCallbackError
│   ├── UnAuthorizedAccountError
│   ├── UnknownRpcClientError
│   └── UnsubscribedAccountError
│
├── ChannelError
│   ├── ChannelCallError
│   ├── ChannelConnectionError
│   ├── ChannelPingTimedOutError
│   ├── UnexpectedChannelMessageError
│   ├── ChannelIncomingMessageError
│   └── UnknownChannelStateError
│
├── CompilerError
│   └── InvalidAuthDataError
│
├── ContractError
│   ├── BytecodeMismatchError
│   ├── DuplicateContractError
│   ├── InactiveContractError
│   ├── InvalidMethodInvocationError
│   ├── MissingContractAddressError
│   ├── MissingContractDefError
│   ├── MissingFunctionNameError
│   ├── NodeInvocationError
│   ├── NoSuchContractFunctionError
│   ├── NotPayableFunctionError
│   ├── MissingEventDefinitionError
│   └── AmbiguousEventDefinitionError
│
├── CryptographyError
│   ├── InvalidChecksumError
│   ├── MerkleTreeHashMismatchError
│   ├── MissingNodeInTreeError
│   ├── UnknownNodeLengthError
│   └── UnknownPathNibbleError
│
├── NodeError
│   ├── DuplicateNodeError
│   └── NodeNotFoundError
│
├── TransactionError
│   ├── DecodeError
│   ├── PayloadLengthError
│   ├── DryRunError
│   ├── IllegalBidFeeError
│   ├── InvalidSignatureError
│   ├── InvalidTxError
│   ├── PrefixNotFoundError
│   ├── SchemaNotFoundError
│   ├── TagNotFoundError
│   └── TxNotInChainError
│
├── WalletError
│   ├── AlreadyConnectedError
│   ├── NoWalletConnectedError
│   └── RpcConnectionError
│
├── RpcError
│   ├── RpcInvalidTransactionError
│   ├── RpcRejectedByUserError
│   ├── RpcUnsupportedProtocolError
│   ├── RpcConnectionDenyError
│   ├── RpcNotAuthorizeError
│   ├── RpcPermissionDenyError
│   └── RpcInternalError

Usage

// import required error classes
import {
  AeSdk,
  Node,
  MemoryAccount,
  ArgumentError,
  InvalidAensNameError,
} from '@aeternity/aepp-sdk';

// setup
const payerAccount = MemoryAccount.generate();
const newUserAccount = MemoryAccount.generate();
const node = new Node('https://testnet.aeternity.io');
const aeSdk = new AeSdk({
  nodes: [{ name: 'testnet', instance: node }],
  accounts: [payerAccount, newUserAccount],
});

// catch exceptions
try {
  const spendTxResult = await aeSdk.spend(-1, newUserAccount.address, { onAccount: payerAccount });
} catch (err) {
  if (err instanceof ArgumentError) {
    console.log(`Amount specified is not valid, ${err.message}`);
  } else if (err instanceof InvalidAensNameError) {
    console.log(`Address specified is not valid, ${err.message}`);
  }
}

// using generic error classes
import { AensError, TransactionError, BaseError } from '@aeternity/aepp-sdk';

try {
  const spendTxResult = await aeSdk.spend(1, 'ak_2tv', { onAccount: payerAccount });
} catch (err) {
  if (err instanceof AensError) {
    // address or AENS related errors
  } else if (err instanceof TransactionError) {
    // transaction errors
  } else if (err instanceof BaseError) {
    // match any errors from the SDK
  }
}