Skip to main content

agent_context/
error.rs

1/// Agent 上下文操作错误类型。
2///
3/// 涵盖两类错误:后端 API 调用失败和本地上下文操作失败。
4/// 使用 [`thiserror`](https://crates.io/crates/thiserror) 派生 `Display` 和 `Error` 实现。
5#[derive(Debug, Clone, PartialEq, thiserror::Error)]
6pub enum AgentError {
7    /// 后端 API 请求失败(网络错误、鉴权失败、速率限制等)。
8    #[error("API 请求失败: {0}")]
9    Api(String),
10    /// 本地上下文操作失败(索引越界、格式转换失败等)。
11    #[error("上下文操作失败: {0}")]
12    Context(String),
13}
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    #[test]
20    fn api_error_message() {
21        let err = AgentError::Api("timeout".into());
22        assert_eq!(err.to_string(), "API 请求失败: timeout");
23    }
24
25    #[test]
26    fn context_error_message() {
27        let err = AgentError::Context("索引越界".into());
28        assert_eq!(err.to_string(), "上下文操作失败: 索引越界");
29    }
30}