Error Handling
Error Classes
HundredXApiError
Thrown for API errors returned by the 100x backend.
typescript
import { HundredXApiError } from '100x-sdk';
try {
await sdk.exchange.placeOrder({ ... });
} catch (error) {
if (error instanceof HundredXApiError) {
console.error('API Error:', error.code, error.message);
}
}Properties:
| Property | Type | Description |
|---|---|---|
code | string | Error code (e.g., "error", "NETWORK_ERROR") |
message | string | Human-readable error message |
name | string | Always "HundredXApiError" |
AuthenticationError
Thrown when trying to use authenticated operations on a read-only SDK instance.
typescript
import { AuthenticationError } from '100x-sdk';
try {
// This will throw if SDK was created with new HundredX() instead of HundredX.create()
await sdk.exchange.placeOrder({ ... });
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Auth Error:', error.message);
}
}Error Format
The 100x backend returns errors wrapped in a standard response:
json
{
"body": null,
"message": "error description",
"status": 400
}The SDK automatically detects errors and throws HundredXApiError.
HTTP Status Codes
| Status | Meaning | Common Cause |
|---|---|---|
400 | Bad Request | Invalid order parameters |
401 | Unauthorized | Invalid credentials |
403 | Forbidden | IP not whitelisted, broker ID mismatch |
404 | Not Found | Order not found |
500 | Internal Server Error | Server-side issue |
Network Errors
If the request fails to reach the server:
typescript
try {
await sdk.info.getOrders();
} catch (error) {
if (error instanceof HundredXApiError && error.code === 'NETWORK_ERROR') {
console.error('Server unreachable');
}
}Best Practices
typescript
import { HundredXApiError, AuthenticationError } from '100x-sdk';
async function placeOrderSafe() {
try {
const order = await sdk.exchange.placeOrder({
marketId: 1,
isBuy: true,
amount: '1.0',
price: '2500.00',
orderType: 'LIMIT',
});
console.log('Order placed:', order.id, order.status);
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Missing credentials:', error.message);
} else if (error instanceof HundredXApiError) {
console.error(`API Error [${error.code}]: ${error.message}`);
} else {
throw error;
}
}
}