> ## Documentation Index
> Fetch the complete documentation index at: https://public-perps-docs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Error Handling

> Handle trading errors with PerpsError and error codes

Trading operations may throw `PerpsError` with numeric error codes. Use `PerpsErrorCode` for type-safe handling:

```typescript theme={null}
import { PerpsError, PerpsErrorCode } from '@lifi/perps-sdk';

try {
  await perps.placeOrder({
    address: userAddress,
    provider: 'hyperliquid',
    market: { marketId: 'BTC', categoryId: 'hyperliquid' },
    side: 'BUY',
    type: 'MARKET',
    size: '0.1',
    price: '95500.00',
  });
} catch (error) {
  if (error instanceof PerpsError) {
    switch (error.code) {
      case PerpsErrorCode.InsufficientMargin:  // 2021
        console.error('Not enough margin. Reduce size or add funds.');
        break;
      case PerpsErrorCode.InsufficientBalance: // 2022
        console.error('Insufficient balance. Deposit more funds.');
        break;
      case PerpsErrorCode.MarketNotFound:      // 2023
        console.error('Invalid asset. Check available assets.');
        break;
      case PerpsErrorCode.AgentUnauthorized:   // 2011
        console.error('Agent not authorized. Complete authorization flow.');
        break;
      case PerpsErrorCode.NonceExpired:        // 2042
        console.error('Payload expired. Retry the operation.');
        break;
      default:
        console.error(`Error ${error.code}: ${error.message}`);
    }
  }
}
```

## Common Trading Errors

| Code | Name                  | Cause                                        |
| ---- | --------------------- | -------------------------------------------- |
| 2021 | `InsufficientMargin`  | Not enough margin for order size/leverage    |
| 2022 | `InsufficientBalance` | Account lacks funds                          |
| 2020 | `ExchangeRejected`    | DEX rejected (invalid params, market closed) |
| 2024 | `OrderNotFound`       | Order ID doesn't exist or already filled     |
| 2042 | `NonceExpired`        | Payload TTL exceeded — retry operation       |

See [Error Codes](/error-codes) for the complete reference.
