Skip to main content

bob_core/
error.rs

1//! # Error Types
2//!
3//! Error types for the Bob Agent Framework.
4//!
5//! This module provides comprehensive error handling with:
6//!
7//! - **`AgentError`**: Top-level error enum wrapping all error types
8//! - **`LlmError`**: Errors from LLM providers
9//! - **`ToolError`**: Errors from tool execution
10//! - **`StoreError`**: Errors from session storage
11//!
12//! ## Error Handling Strategy
13//!
14//! All errors use [`thiserror`] for ergonomic error definitions and implement:
15//! - `std::error::Error` for compatibility
16//! - `Display` for user-friendly messages
17//! - `From` for automatic conversion
18//!
19//! ## Example
20//!
21//! ```rust,ignore
22//! use bob_core::error::{AgentError, LlmError};
23//!
24//! fn handle_error(err: AgentError) {
25//!     match err {
26//!         AgentError::Llm(e) => eprintln!("LLM error: {}", e),
27//!         AgentError::Tool(e) => eprintln!("Tool error: {}", e),
28//!         AgentError::Policy(msg) => eprintln!("Policy violation: {}", msg),
29//!         AgentError::Timeout => eprintln!("Operation timed out"),
30//!         _ => eprintln!("Other error: {}", err),
31//!     }
32//! }
33//! ```
34
35/// Top-level agent error.
36#[derive(thiserror::Error, Debug)]
37pub enum AgentError {
38    #[error("LLM provider error: {0}")]
39    Llm(#[from] LlmError),
40
41    #[error("Tool execution error: {0}")]
42    Tool(#[from] ToolError),
43
44    #[error("Policy violation: {0}")]
45    Policy(String),
46
47    #[error("configuration error: {0}")]
48    Config(String),
49
50    #[error("Store error: {0}")]
51    Store(#[from] StoreError),
52
53    #[error("Cost meter error: {0}")]
54    Cost(#[from] CostError),
55
56    #[error("timeout")]
57    Timeout,
58
59    #[error("guard exceeded: {reason:?}")]
60    GuardExceeded { reason: crate::types::GuardReason },
61
62    #[error(transparent)]
63    Internal(#[from] Box<dyn std::error::Error + Send + Sync>),
64}
65
66/// LLM adapter errors.
67#[derive(thiserror::Error, Debug)]
68pub enum LlmError {
69    #[error("provider error: {0}")]
70    Provider(String),
71
72    #[error("rate limited")]
73    RateLimited,
74
75    #[error("context length exceeded")]
76    ContextLengthExceeded,
77
78    #[error("stream error: {0}")]
79    Stream(String),
80
81    #[error(transparent)]
82    Other(#[from] Box<dyn std::error::Error + Send + Sync>),
83}
84
85/// Tool execution errors.
86#[derive(thiserror::Error, Debug)]
87pub enum ToolError {
88    #[error("tool not found: {name}")]
89    NotFound { name: String },
90
91    #[error("tool execution failed: {0}")]
92    Execution(String),
93
94    #[error("tool timeout: {name}")]
95    Timeout { name: String },
96
97    #[error(transparent)]
98    Other(#[from] Box<dyn std::error::Error + Send + Sync>),
99}
100
101/// Session store errors.
102#[derive(thiserror::Error, Debug)]
103pub enum StoreError {
104    #[error("serialization error: {0}")]
105    Serialization(String),
106
107    #[error("storage backend error: {0}")]
108    Backend(String),
109
110    #[error(transparent)]
111    Other(#[from] Box<dyn std::error::Error + Send + Sync>),
112}
113
114/// Cost meter errors.
115#[derive(thiserror::Error, Debug)]
116pub enum CostError {
117    #[error("budget exceeded: {0}")]
118    BudgetExceeded(String),
119
120    #[error("cost backend error: {0}")]
121    Backend(String),
122
123    #[error(transparent)]
124    Other(#[from] Box<dyn std::error::Error + Send + Sync>),
125}