Error Handling
All SDK methods return a consistent response pattern. For method-specific errors, see the "Common Errors" section in each method's documentation.
Response Structure
TypeScript:
{
success: boolean;
data?: any;
error?: string;
errorMessage?: string;
errorDetails?: object;
}Python:
{
success: bool
data: any | None
error: str | None
error_message: str | None # Note: Python uses snake_case
error_details: dict | None
}Checking Results
Always check success before using data:
TypeScript:
const result = await agent.memory.get("key");
if (result.success && result.data) {
// Safe to use result.data
await agent.log(`Value: ${result.data.value}`);
} else {
// Handle error
await agent.log(`Error: ${result.error}`, { error: true });
}Python:
result = agent.memory.get("key")
if result.success and result.data:
# Safe to use result.data
agent.log(f"Value: {result.data.value}")
else:
# Handle error
agent.log(f"Error: {result.error}", error=True)Uncaught Exceptions
The SDK automatically handles uncaught exceptions in your run and stop functions:
TypeScript:
async function run(agent: AgentContext): Promise<void> {
// This typo will be caught and logged automatically
await agent.memmory.set("key", "value"); // TypeError
}Python:
def run(agent: AgentContext) -> None:
# This typo will be caught and logged automatically
agent.memmory.set("key", "value") # Typo - auto-caughtNo try/catch needed, and the agent won't crash. Circuit will mark the execution as failed and log the error.
When to Use Try/Catch
Custom business logic that throws
SDK method calls (use .success checks)
Operations requiring custom recovery
Top level errors (auto-caught by SDK)
Parsing/calculations with external data
Common Error Patterns
"Key not found"
Memory key doesn't exist
Check if key exists first or handle missing keys
"Invalid request"
Bad parameters
Validate inputs before calling SDK methods
"Quote failed"
Amount too small, no liquidity
Increase amount, adjust slippage, or validate quote
"Transaction failed"
Insufficient balance/gas
Check balances via currentPositions before transactions
Best Practices
Always check
successbefore usingdataLog errors with
{ error: true }for visibilityFail gracefully - return early on errors instead of continuing
Don't wrap everything in try/catch - the SDK handles this
Validate inputs before making SDK calls
Last updated