strands_agents/types/
errors.rs

1//! Error types for the Strands SDK.
2
3use thiserror::Error;
4
5/// The main error type for Strands SDK operations.
6#[derive(Error, Debug)]
7pub enum StrandsError {
8    /// An error occurred during model invocation.
9    #[error("Model error: {message}")]
10    ModelError {
11        message: String,
12        #[source]
13        source: Option<Box<dyn std::error::Error + Send + Sync>>,
14    },
15
16    /// The model is being throttled due to rate limits.
17    #[error("Model throttled: {message}")]
18    ModelThrottled { message: String },
19
20    /// An error occurred during tool execution.
21    #[error("Tool execution error in '{tool_name}': {message}")]
22    ToolError { tool_name: String, message: String },
23
24    /// The requested tool was not found in the registry.
25    #[error("Tool not found: {tool_name}")]
26    ToolNotFound { tool_name: String },
27
28    /// Tool validation failed.
29    #[error("Tool validation error: {message}")]
30    ToolValidationError { message: String },
31
32    /// Invalid tool name.
33    #[error("Invalid tool name '{name}': {reason}")]
34    InvalidToolName { name: String, reason: String },
35
36    /// Duplicate tool name in registry.
37    #[error("Duplicate tool name: {name}")]
38    DuplicateToolName { name: String },
39
40    /// Invalid tool use - tool not found in registry.
41    #[error("Invalid tool use: '{name}' not found. Available tools: {available_tools:?}")]
42    InvalidToolUseName { name: String, available_tools: Vec<String> },
43
44    /// The context window has been exceeded.
45    #[error("Context window overflow: {message}")]
46    ContextWindowOverflow { message: String },
47
48    /// The model reached the maximum token limit.
49    #[error("Max tokens reached")]
50    MaxTokensReached,
51
52    /// Content was filtered by safety systems.
53    #[error("Content filtered: {message}")]
54    ContentFiltered { message: String },
55
56    /// A guardrail intervention occurred.
57    #[error("Guardrail intervention: {message}")]
58    GuardrailIntervention { message: String },
59
60    /// An error occurred in the event loop.
61    #[error("Event loop error: {message}")]
62    EventLoopError { message: String },
63
64    /// A session-related error occurred.
65    #[error("Session error: {message}")]
66    SessionError { message: String },
67
68    /// JSON serialization/deserialization error.
69    #[error("Serialization error: {0}")]
70    SerializationError(#[from] serde_json::Error),
71
72    /// Configuration error.
73    #[error("Configuration error: {message}")]
74    ConfigurationError { message: String },
75
76    /// Invalid input was provided.
77    #[error("Invalid input: {message}")]
78    InvalidInput { message: String },
79
80    /// An internal error occurred.
81    #[error("Internal error: {message}")]
82    InternalError { message: String },
83
84    /// Structured output parsing failed.
85    #[error("Structured output error: {message}")]
86    StructuredOutputError { message: String },
87
88    /// The agent was interrupted.
89    #[error("Agent interrupted: {message}")]
90    Interrupted { message: String },
91
92    /// An error occurred in multi-agent orchestration.
93    #[error("Multi-agent error: {message}")]
94    MultiAgentError { message: String },
95
96    /// A network error occurred.
97    #[error("Network error: {0}")]
98    NetworkError(String),
99
100    /// An AWS-specific error occurred.
101    #[error("AWS error: {0}")]
102    AwsError(String),
103
104    /// MCP client initialization error.
105    #[error("MCP client initialization error: {message}")]
106    MCPClientInitializationError { message: String },
107
108    /// Tool provider error.
109    #[error("Tool provider error: {message}")]
110    ToolProviderError { message: String },
111
112    /// Feature not yet implemented.
113    #[error("Not implemented: {feature}")]
114    NotImplemented { feature: String },
115}
116
117impl StrandsError {
118    pub fn model_error(message: impl Into<String>) -> Self {
119        Self::ModelError { message: message.into(), source: None }
120    }
121
122    pub fn model_error_with_source(
123        message: impl Into<String>,
124        source: impl std::error::Error + Send + Sync + 'static,
125    ) -> Self {
126        Self::ModelError { message: message.into(), source: Some(Box::new(source)) }
127    }
128
129    pub fn tool_error(tool_name: impl Into<String>, message: impl Into<String>) -> Self {
130        Self::ToolError { tool_name: tool_name.into(), message: message.into() }
131    }
132
133    pub fn config_error(message: impl Into<String>) -> Self {
134        Self::ConfigurationError { message: message.into() }
135    }
136
137    pub fn invalid_input(message: impl Into<String>) -> Self {
138        Self::InvalidInput { message: message.into() }
139    }
140
141    /// Returns true if the error is retryable.
142    pub fn is_retryable(&self) -> bool {
143        matches!(self, Self::ModelThrottled { .. } | Self::NetworkError(_))
144    }
145}
146
147/// A type alias for Results using StrandsError.
148pub type Result<T> = std::result::Result<T, StrandsError>;