1use std::time::Duration;
4use thiserror::Error;
5
6#[derive(Debug, Clone, Error)]
21#[non_exhaustive]
22pub enum SandboxError {
23 #[error("execution timed out after {timeout:?}")]
25 Timeout {
26 timeout: Duration,
28 },
29
30 #[error("memory limit exceeded: {limit_mb} MB")]
32 MemoryExceeded {
33 limit_mb: u32,
35 },
36
37 #[error("execution failed: {0}")]
39 ExecutionFailed(String),
40
41 #[error("invalid request: {0}")]
43 InvalidRequest(String),
44
45 #[error("backend unavailable: {0}")]
47 BackendUnavailable(String),
48
49 #[error("enforcer '{enforcer}' failed: {message}")]
51 EnforcerFailed {
52 enforcer: String,
54 message: String,
56 },
57
58 #[error("enforcer '{enforcer}' unavailable: {message}")]
60 EnforcerUnavailable {
61 enforcer: String,
63 message: String,
65 },
66
67 #[error("policy violation: {0}")]
69 PolicyViolation(String),
70
71 #[error(
73 "enforcer '{enforcer}' cannot enforce {requirement}, so the command does not run with \
74 less confinement than its policy asks for; choose a mode the enforcer supports (see \
75 `SandboxEnforcer::network_support`) or an enforcer that supports this one"
76 )]
77 Unenforceable {
78 enforcer: String,
80 requirement: String,
82 },
83
84 #[cfg(feature = "workspace")]
87 #[error("provisioning failed for '{resource}': {reason}. {suggestion}")]
88 ProvisionFailed {
89 resource: String,
91 reason: String,
93 suggestion: String,
95 },
96
97 #[cfg(feature = "workspace")]
99 #[error("session '{handle}' not found. It may have been stopped or expired.")]
100 SessionNotFound {
101 handle: String,
103 },
104
105 #[cfg(feature = "workspace")]
107 #[error("snapshot '{id}' not found. It may have been deleted or expired.")]
108 SnapshotNotFound {
109 id: String,
111 },
112
113 #[cfg(feature = "workspace")]
115 #[error("path traversal rejected: '{path}' escapes workspace root. Use relative paths only.")]
116 PathTraversal {
117 path: String,
119 },
120
121 #[cfg(feature = "workspace")]
123 #[error("Docker unavailable: {reason}. Ensure Docker daemon is running and accessible.")]
124 DockerUnavailable {
125 reason: String,
127 },
128
129 #[cfg(feature = "workspace")]
131 #[error(
132 "session timed out after {timeout:?}. Consider increasing session_timeout in SandboxConfig."
133 )]
134 SessionTimeout {
135 timeout: Duration,
137 },
138
139 #[cfg(feature = "workspace")]
142 #[error(
143 "{provider} does not support {operation}; check `SandboxClient::capabilities` before \
144 calling it"
145 )]
146 Unsupported {
147 provider: String,
149 operation: String,
151 },
152
153 #[cfg(feature = "workspace")]
155 #[error("{provider} {operation} failed{}: {message}", status.map(|s| format!(" with HTTP {s}")).unwrap_or_default())]
156 Remote {
157 provider: String,
159 operation: String,
161 status: Option<u16>,
164 message: String,
166 },
167}
168
169impl From<std::io::Error> for SandboxError {
170 fn from(err: std::io::Error) -> Self {
171 SandboxError::ExecutionFailed(format!("I/O error: {err}"))
172 }
173}
174
175impl From<SandboxError> for adk_core::AdkError {
176 fn from(err: SandboxError) -> Self {
177 use adk_core::{ErrorCategory, ErrorComponent};
178 let (category, code) = match &err {
179 SandboxError::Timeout { .. } => (ErrorCategory::Timeout, "code.sandbox_timeout"),
180 SandboxError::MemoryExceeded { .. } => (ErrorCategory::Internal, "code.sandbox_memory"),
181 SandboxError::ExecutionFailed(_) => (ErrorCategory::Internal, "code.sandbox_execution"),
182 SandboxError::InvalidRequest(_) => {
183 (ErrorCategory::InvalidInput, "code.sandbox_invalid_request")
184 }
185 SandboxError::BackendUnavailable(_) => {
186 (ErrorCategory::Unavailable, "code.sandbox_unavailable")
187 }
188 SandboxError::EnforcerFailed { .. } => {
189 (ErrorCategory::Internal, "code.sandbox_enforcer_failed")
190 }
191 SandboxError::EnforcerUnavailable { .. } => {
192 (ErrorCategory::Unavailable, "code.sandbox_enforcer_unavailable")
193 }
194 SandboxError::PolicyViolation(_) => {
195 (ErrorCategory::InvalidInput, "code.sandbox_policy_violation")
196 }
197 SandboxError::Unenforceable { .. } => {
198 (ErrorCategory::Unsupported, "code.sandbox_unenforceable")
199 }
200 #[cfg(feature = "workspace")]
201 SandboxError::ProvisionFailed { .. } => {
202 (ErrorCategory::Internal, "code.sandbox_provision_failed")
203 }
204 #[cfg(feature = "workspace")]
205 SandboxError::SessionNotFound { .. } => {
206 (ErrorCategory::NotFound, "code.sandbox_session_not_found")
207 }
208 #[cfg(feature = "workspace")]
209 SandboxError::SnapshotNotFound { .. } => {
210 (ErrorCategory::NotFound, "code.sandbox_snapshot_not_found")
211 }
212 #[cfg(feature = "workspace")]
213 SandboxError::PathTraversal { .. } => {
214 (ErrorCategory::InvalidInput, "code.sandbox_path_traversal")
215 }
216 #[cfg(feature = "workspace")]
217 SandboxError::DockerUnavailable { .. } => {
218 (ErrorCategory::Unavailable, "code.sandbox_docker_unavailable")
219 }
220 #[cfg(feature = "workspace")]
221 SandboxError::SessionTimeout { .. } => {
222 (ErrorCategory::Timeout, "code.sandbox_session_timeout")
223 }
224 #[cfg(feature = "workspace")]
225 SandboxError::Unsupported { .. } => {
226 (ErrorCategory::Unsupported, "code.sandbox_unsupported")
227 }
228 #[cfg(feature = "workspace")]
229 SandboxError::Remote { status, .. } => {
230 let category = match status {
231 Some(401) => ErrorCategory::Unauthorized,
232 Some(403) => ErrorCategory::Forbidden,
233 Some(404) => ErrorCategory::NotFound,
234 Some(408 | 504) => ErrorCategory::Timeout,
235 Some(409) => ErrorCategory::Conflict,
236 Some(429) => ErrorCategory::RateLimited,
237 Some(400..=499) => ErrorCategory::InvalidInput,
238 Some(_) | None => ErrorCategory::Unavailable,
239 };
240 (category, "code.sandbox_remote")
241 }
242 };
243 #[cfg(feature = "workspace")]
244 let remote = match &err {
245 SandboxError::Remote { provider, status, .. } => Some((provider.clone(), *status)),
246 _ => None,
247 };
248 let error = adk_core::AdkError::new(ErrorComponent::Code, category, code, err.to_string())
249 .with_source(err);
250 #[cfg(feature = "workspace")]
251 let error = match remote {
252 Some((provider, Some(status))) => {
253 error.with_provider(provider).with_upstream_status(status)
254 }
255 Some((provider, None)) => error.with_provider(provider),
256 None => error,
257 };
258 error
259 }
260}