Skip to main content

agent_tools/error/
mod.rs

1mod tools;
2
3use std::fmt;
4use std::io;
5
6/// Core error struct for the workspace.
7/// This hides the internal details of specific errors (like ToolError).
8#[derive(Debug)]
9pub struct Error {
10    inner: Box<ErrorKind>,
11}
12
13/// Internal enum representing all possible error variants.
14/// This is not exposed publicly, preventing users from matching on internal details.
15#[derive(Debug, thiserror::Error)]
16enum ErrorKind {
17    #[error(transparent)]
18    Tools(#[from] tools::ToolError),
19}
20
21impl fmt::Display for Error {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        write!(f, "{}", self.inner)
24    }
25}
26
27impl std::error::Error for Error {
28    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
29        self.inner.source()
30    }
31}
32
33impl Error {
34    /// Creates an IO error associated with tools execution.
35    pub fn tool_io(err: io::Error) -> Self {
36        Error {
37            inner: Box::new(ErrorKind::Tools(tools::ToolError::Io(err))),
38        }
39    }
40    
41    /// Creates a command failure error associated with tools execution.
42    pub fn tool_cmd_failed(code: i32) -> Self {
43        Error {
44            inner: Box::new(ErrorKind::Tools(tools::ToolError::CommandFailed(code))),
45        }
46    }
47
48    /// Creates a timeout error associated with tools execution.
49    pub fn tool_timeout() -> Self {
50        Error {
51            inner: Box::new(ErrorKind::Tools(tools::ToolError::Timeout)),
52        }
53    }
54}
55
56/// Result type alias using the opaque Error.
57pub type Result<T> = std::result::Result<T, Error>;