errors — Error types¶
All errors raised by the SDK are subclasses of O2Error. The error
hierarchy maps directly to the error codes defined in the
O2 Exchange API.
Base exception¶
Error code reference¶
Code |
Exception class |
Category |
Recovery strategy |
|---|---|---|---|
1000 |
|
General |
Retry with exponential backoff. |
1001 |
|
General |
Fix the request format. |
1002 |
|
General |
Fix the request body. |
1003 |
|
General |
Wait 3–5 seconds, then retry. The SDK handles this automatically with up to 3 retries. |
1004 |
|
General |
Region not allowed. |
2000 |
|
Market |
Verify the market ID or pair string. |
2001 |
|
Market |
Wait for the market to resume. |
2002 |
|
Market |
Market already exists. |
3000 |
|
Order |
Verify the order ID. |
3001 |
|
Order |
The order is already closed or canceled. |
3002 |
|
Order |
Check price, quantity, and order type. |
4000 |
|
Account/Session |
Check your signing method ( |
4001 |
|
Account/Session |
Create a new session. |
4002 |
|
Account/Session |
Call |
4003 |
|
Account/Session |
Account needs whitelisting (done automatically by
|
5000 |
|
Trade |
Verify the trade ID. |
5001 |
|
Trade |
Invalid trade count parameter. |
6000 |
|
WebSocket |
Already subscribed to this topic. |
6001 |
|
WebSocket |
Reduce the number of active subscriptions. |
6002 |
|
WebSocket |
General subscription error. |
7000 |
|
Validation |
Check the amount value. |
7001 |
|
Validation |
Check the time range parameters. |
7002 |
|
Validation |
Check pagination parameters. |
7003 |
|
Validation |
Include at least one action. |
7004 |
|
Validation |
Maximum 5 actions per request. |
8000 |
|
Block/Events |
Block not found. |
8001 |
|
Block/Events |
Events not found for the specified block. |
Special error types¶
- class o2_sdk.errors.SessionExpired[source]¶
Client-side error raised when the session has expired before submitting an action. Create a new session.
This is detected locally by the SDK (no network call needed) by comparing the session’s expiry timestamp against the current time.
- class o2_sdk.errors.OnChainRevert[source]¶
An on-chain transaction revert.
This error has no error code — it is distinguished by having a
messageandreasonbut nocodefield. Common revert reasons include:NotEnoughBalance— Insufficient funds for the operation.TraderNotWhiteListed— Account is not whitelisted.InvalidPrice— Price violates on-chain constraints.
from o2_sdk import OnChainRevert try: result = await client.create_order(...) except OnChainRevert as e: print(f"Revert: {e.message}, reason: {e.reason}")
Error handling patterns¶
from o2_sdk import (
O2Error, OrderSide,
InvalidSignature,
RateLimitExceeded,
OnChainRevert,
SessionExpired,
)
try:
result = await client.create_order(
"fFUEL/fUSDC", OrderSide.BUY, 0.02, 100.0
)
except SessionExpired:
# Create a new session
session = await client.create_session(owner=owner, markets=["fFUEL/fUSDC"])
result = await client.create_order(
"fFUEL/fUSDC", OrderSide.BUY, 0.02, 100.0
)
except InvalidSignature:
# Check signing logic
print("Signature verification failed")
except RateLimitExceeded:
# SDK retries automatically, but you can add extra backoff
await asyncio.sleep(5)
except OnChainRevert as e:
print(f"On-chain revert: {e.reason}")
except O2Error as e:
print(f"Error {e.code}: {e.message}")
Helper function¶
- o2_sdk.errors.raise_for_error(data)[source]¶
Inspect a raw API response dict and raise the appropriate exception.
Handles both pre-flight validation errors (with
code) and on-chain revert errors (withmessage+reason, nocode). Does nothing if the response contains atx_id(success).- Parameters:
data (dict) – Raw API response dict.