Skip to main content

agent_code_lib/
error.rs

1//! Unified error types for the application.
2//!
3//! Each subsystem defines specific error variants that compose into
4//! the top-level `Error` enum. Tool errors and API errors are recoverable
5//! within the agent loop; other errors propagate to the caller.
6
7use thiserror::Error;
8
9/// Top-level error type.
10#[derive(Debug, Error)]
11pub enum Error {
12    #[error(transparent)]
13    Llm(#[from] LlmError),
14
15    #[error(transparent)]
16    Tool(#[from] ToolError),
17
18    #[error(transparent)]
19    Permission(#[from] PermissionError),
20
21    #[error(transparent)]
22    Config(#[from] ConfigError),
23
24    #[error(transparent)]
25    Io(#[from] std::io::Error),
26
27    #[error("{0}")]
28    Other(String),
29}
30
31/// LLM API errors.
32#[derive(Debug, Error)]
33pub enum LlmError {
34    #[error("HTTP request failed: {0}")]
35    Http(String),
36
37    #[error("API error (status {status}): {body}")]
38    Api { status: u16, body: String },
39
40    #[error("Rate limited, retry after {retry_after_ms}ms")]
41    RateLimited { retry_after_ms: u64 },
42
43    #[error("Stream interrupted")]
44    StreamInterrupted,
45
46    #[error("Invalid response: {0}")]
47    InvalidResponse(String),
48
49    #[error("Authentication failed: {0}")]
50    AuthError(String),
51
52    #[error("Context window exceeded ({tokens} tokens)")]
53    ContextOverflow { tokens: usize },
54}
55
56/// Tool execution errors.
57#[derive(Debug, Error)]
58pub enum ToolError {
59    #[error("Permission denied: {0}")]
60    PermissionDenied(String),
61
62    #[error("Tool execution failed: {0}")]
63    ExecutionFailed(String),
64
65    #[error("Invalid input: {0}")]
66    InvalidInput(String),
67
68    #[error("Tool not found: {0}")]
69    NotFound(String),
70
71    #[error("IO error: {0}")]
72    Io(#[from] std::io::Error),
73
74    #[error("Operation cancelled")]
75    Cancelled,
76
77    #[error("Timeout after {0}ms")]
78    Timeout(u64),
79}
80
81/// Permission system errors.
82#[derive(Debug, Error)]
83pub enum PermissionError {
84    #[error("Permission denied by rule: {0}")]
85    DeniedByRule(String),
86
87    #[error("User denied permission for {tool}: {reason}")]
88    UserDenied { tool: String, reason: String },
89}
90
91/// Configuration errors.
92#[derive(Debug, Error)]
93pub enum ConfigError {
94    #[error("Config file error: {0}")]
95    FileError(String),
96
97    #[error("Invalid config value: {0}")]
98    InvalidValue(String),
99
100    #[error("TOML parse error: {0}")]
101    ParseError(#[from] toml::de::Error),
102}
103
104/// Convenience alias.
105pub type Result<T> = std::result::Result<T, Error>;