Error handling

The SDK throws HyperliquidError and its subclasses. Each subclass represents a specific failure category, such as validation, transport, API, or wallet errors.

Error hierarchy

Error
└─ HyperliquidError
   ├─ ValidationError
   ├─ AbstractWalletError
   ├─ TransportError
   │  ├─ HttpRequestError
   │  └─ WebSocketRequestError
   └─ ApiRequestError

Transport errors

These errors are thrown when the request itself fails, such as network issues, timeouts, or non-JSON responses.

HttpRequestError

This error is thrown when an HTTP request fails, such as non-200 status codes, non-JSON responses, or network-level failures.

Has response (the Responsearrow-up-right object) when the server responded, undefined for network-level failures.

import { HttpRequestError } from "@nktkas/hyperliquid";

try {
  await client.allMids();
} catch (error) {
  if (error instanceof HttpRequestError) {
    console.error(error.message); // "429 Too Many Requests - ..."
    console.error(error.response?.status); // 429
    console.error(await error.response?.text()); // response body
  }
}

WebSocketRequestError

This error is thrown when a WebSocket request or subscription fails, such as connection failures, server errors, or exceeded subscription limits.

API errors

ApiRequestError is thrown when the Hyperliquid API processes the request but returns an error, such as insufficient margin, invalid price, or unknown wallet address.

Has response with the full API response object:

Wallet errors

AbstractWalletError is thrown when a wallet operation fails, such as signature creation, address retrieval, or chain ID detection. The original error is in cause.

Validation errors

ValidationError is thrown when request parameters fail schema validation before sending.

The cause property contains the original ValiErrorarrow-up-right with detailed issue information:

Timeouts and cancellation

Both transports use AbortSignalarrow-up-right internally, so timeouts and user-initiated cancellations produce a DOMExceptionarrow-up-right in cause:

Catch all errors

Last updated