smolagents_rs/
errors.rs

1use std::fmt;
2
3#[derive(Debug, Clone)]
4pub enum AgentError {
5    Parsing(String),
6    Execution(String),
7    MaxSteps(String),
8    Generation(String),
9}
10
11impl std::error::Error for AgentError {}
12
13impl AgentError {
14    pub fn message(&self) -> &str {
15        match self {
16            Self::Parsing(msg) => msg,
17            Self::Execution(msg) => msg,
18            Self::MaxSteps(msg) => msg,
19            Self::Generation(msg) => msg,
20        }
21    }
22}
23impl std::fmt::Display for AgentError {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        match self {
26            Self::Parsing(msg) => write!(f, "{}", msg),
27            Self::Execution(msg) => write!(f, "{}", msg),
28            Self::MaxSteps(msg) => write!(f, "{}", msg),
29            Self::Generation(msg) => write!(f, "{}", msg),
30        }
31    }
32}
33
34pub type AgentParsingError = AgentError;
35pub type AgentExecutionError = AgentError;
36pub type AgentMaxStepsError = AgentError;
37pub type AgentGenerationError = AgentError;
38
39// Custom error type for interpreter
40#[derive(Debug, PartialEq)]
41pub enum InterpreterError {
42    SyntaxError(String),
43    RuntimeError(String),
44    FinalAnswer(String),
45    OperationLimitExceeded,
46    UnauthorizedImport(String),
47    UnsupportedOperation(String),
48}
49
50impl fmt::Display for InterpreterError {
51    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52        match self {
53            InterpreterError::SyntaxError(msg) => write!(f, "Syntax Error: {}", msg),
54            InterpreterError::RuntimeError(msg) => write!(f, "Runtime Error: {}", msg),
55            InterpreterError::FinalAnswer(msg) => write!(f, "Final Answer: {}", msg),
56            InterpreterError::OperationLimitExceeded => write!(
57                f,
58                "Operation limit exceeded. Possible infinite loop detected."
59            ),
60            InterpreterError::UnauthorizedImport(module) => {
61                write!(f, "Unauthorized import of module: {}", module)
62            }
63            InterpreterError::UnsupportedOperation(op) => {
64                write!(f, "Unsupported operation: {}", op)
65            }
66        }
67    }
68}
69