strands_agents/types/
errors.rs1use thiserror::Error;
4
5#[derive(Error, Debug)]
7pub enum StrandsError {
8 #[error("Model error: {message}")]
10 ModelError {
11 message: String,
12 #[source]
13 source: Option<Box<dyn std::error::Error + Send + Sync>>,
14 },
15
16 #[error("Model throttled: {message}")]
18 ModelThrottled { message: String },
19
20 #[error("Tool execution error in '{tool_name}': {message}")]
22 ToolError { tool_name: String, message: String },
23
24 #[error("Tool not found: {tool_name}")]
26 ToolNotFound { tool_name: String },
27
28 #[error("Tool validation error: {message}")]
30 ToolValidationError { message: String },
31
32 #[error("Invalid tool name '{name}': {reason}")]
34 InvalidToolName { name: String, reason: String },
35
36 #[error("Duplicate tool name: {name}")]
38 DuplicateToolName { name: String },
39
40 #[error("Invalid tool use: '{name}' not found. Available tools: {available_tools:?}")]
42 InvalidToolUseName { name: String, available_tools: Vec<String> },
43
44 #[error("Context window overflow: {message}")]
46 ContextWindowOverflow { message: String },
47
48 #[error("Max tokens reached")]
50 MaxTokensReached,
51
52 #[error("Content filtered: {message}")]
54 ContentFiltered { message: String },
55
56 #[error("Guardrail intervention: {message}")]
58 GuardrailIntervention { message: String },
59
60 #[error("Event loop error: {message}")]
62 EventLoopError { message: String },
63
64 #[error("Session error: {message}")]
66 SessionError { message: String },
67
68 #[error("Serialization error: {0}")]
70 SerializationError(#[from] serde_json::Error),
71
72 #[error("Configuration error: {message}")]
74 ConfigurationError { message: String },
75
76 #[error("Invalid input: {message}")]
78 InvalidInput { message: String },
79
80 #[error("Internal error: {message}")]
82 InternalError { message: String },
83
84 #[error("Structured output error: {message}")]
86 StructuredOutputError { message: String },
87
88 #[error("Agent interrupted: {message}")]
90 Interrupted { message: String },
91
92 #[error("Multi-agent error: {message}")]
94 MultiAgentError { message: String },
95
96 #[error("Network error: {0}")]
98 NetworkError(String),
99
100 #[error("AWS error: {0}")]
102 AwsError(String),
103
104 #[error("MCP client initialization error: {message}")]
106 MCPClientInitializationError { message: String },
107
108 #[error("Tool provider error: {message}")]
110 ToolProviderError { message: String },
111
112 #[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 pub fn is_retryable(&self) -> bool {
143 matches!(self, Self::ModelThrottled { .. } | Self::NetworkError(_))
144 }
145}
146
147pub type Result<T> = std::result::Result<T, StrandsError>;