Error Handling
Handle errors gracefully in your agents
Handle errors gracefully to build robust agent applications.
Quick Start
- Handle Errors
rust use praisonai::Agent; let agent = Agent::new() .name("Assistant") .build()?; match agent.chat("Hello").await { Ok(response) => println!("{}", response), Err(e) => println!("Error: {}", e), } - With Retries
rust use praisonai::{Agent, ExecutionConfig}; let config = ExecutionConfig::new() .max_retries(3); let agent = Agent::new() .name("Reliable Bot") .execution(config) .build()?;
Error Types
| Error | Cause | Solution |
|---|---|---|
ApiError | API call failed | Check API key, retry |
TimeoutError | Request too slow | Increase timeout |
ToolError | Tool execution failed | Check tool logic |
ConfigError | Invalid configuration | Check config values |
Common Patterns
Graceful Fallback
let response = agent.chat("Question").await
.unwrap_or_else(|_| "Sorry, I encountered an error.".to_string());Logging Errors
if let Err(e) = agent.chat("Question").await {
log::error!("Agent error: {}", e);
// Handle gracefully
}Best Practices
Always handle errors
Never use unwrap() in production code.
Set appropriate retries
3 retries is usually enough for transient failures.
Log errors for debugging
Keep detailed logs to diagnose issues later.