Skip to content

Error Handling

All API calls return Result<T, ErrorCode> types.

Error Structure

ts
type ErrorCode = variant {
  AccountError: AccountError;
  AssetError: AssetError;
  TradingError: TradingError;
  WalletError: WalletError;
  SystemError: SystemError;
  GuardError: GuardError;
  InternalError: text;
};

Trading Errors

ErrorDescription
MarketNotFoundInvalid market_id
OrderNotFoundOrder doesn't exist
InvalidPricePrice is zero or invalid
InvalidQuantityQuantity is zero or invalid
ValidationFailedOrder validation failed
MarketAlreadyExistsMarket already registered
SettlementFailedSettlement processing failed
UnauthorizedNot authorized for this operation
OrderBookErrorMatching engine errors

OrderBook Errors

ErrorDescription
MarketClosedMarket is paused
OrderNotFoundOrder not in book
OrderAlreadyExistsDuplicate order ID
SelfTradeDetectedSelf-trade prevention triggered
InsufficientLiquidityNot enough liquidity for market order
RiskLimitExceededOrder exceeds risk limits
ValidationErrorOrder validation failed

Wallet Errors

ErrorDescription
InsufficientBalanceNot enough available funds
InvalidAddressInvalid withdrawal address
InvalidAmountAmount out of valid range
InvalidWithdrawalAmountOutside min/max limits
UnsupportedAssetAsset not supported
WithdrawalNotFoundWithdrawal ID not found
TooManyPendingWithdrawalsRate limit exceeded
ArithmeticOverflowCalculation overflow

System Errors

ErrorDescription
AnonymousCallerAnonymous principal not allowed
UnauthorizedNot authorized
NotControllerController access required
NotAllowlistedPrincipal not in allowlist
UnderMaintenanceSystem in maintenance mode
RateLimitExceededToo many requests
ConcurrentLimitExceededToo many concurrent operations

TypeScript Error Handling

typescript
try {
  const response = await backend.place_order(orderRequest)

  if ('Ok' in response) {
    console.log('Order placed:', response.Ok.order_id)
  } else {
    const error = response.Err

    if ('TradingError' in error) {
      const tradingError = error.TradingError

      if ('InsufficientBalance' in tradingError) {
        console.error('Not enough funds')
      } else if ('MarketNotFound' in tradingError) {
        console.error('Invalid market')
      } else if ('OrderBookError' in tradingError) {
        const obError = tradingError.OrderBookError
        if ('SelfTradeDetected' in obError) {
          console.error('Order would trade against yourself')
        }
      }
    } else if ('SystemError' in error) {
      const sysError = error.SystemError

      if ('RateLimitExceeded' in sysError) {
        const { limit, window_seconds } = sysError.RateLimitExceeded
        console.error(`Rate limited: ${limit} requests per ${window_seconds}s`)
      } else if ('UnderMaintenance' in sysError) {
        console.error('System is under maintenance')
      }
    } else if ('WalletError' in error) {
      if ('InsufficientBalance' in error.WalletError) {
        console.error('Insufficient balance')
      }
    }
  }
} catch (error) {
  // Network error or other unexpected error
  console.error('Network error:', error)
}

SDK Error Handling

Using the @vortum/sdk:

typescript
import { OnexError } from '@vortum/sdk'

try {
  await client.trading.placeOrder(request)
} catch (error) {
  if (error instanceof OnexError) {
    if (error.isTradingError('InsufficientBalance')) {
      // Handle insufficient balance
    } else if (error.isSystemError('RateLimitExceeded')) {
      // Handle rate limit
    } else if (error.isWalletError('InvalidAddress')) {
      // Handle invalid address
    }

    // Get error details
    console.error(error.message)
    console.error(error.code)
  }
}

Best Practices

  1. Always check for errors - Don't assume success
  2. Handle specific errors - Provide appropriate user feedback
  3. Retry transient errors - Rate limits, network issues
  4. Log unexpected errors - For debugging
  5. Don't expose internal errors - Show user-friendly messages