Skip to main content

forge_sandbox/
error.rs

1//! Error types for the Forge sandbox.
2
3use thiserror::Error;
4
5/// Errors that can occur during sandbox execution.
6#[derive(Debug, Error)]
7pub enum SandboxError {
8    /// Code failed validation checks.
9    #[error("code validation failed: {reason}")]
10    ValidationFailed {
11        /// What went wrong.
12        reason: String,
13    },
14
15    /// Code exceeds the configured maximum size.
16    #[error("code exceeds maximum size of {max} bytes (got {actual})")]
17    CodeTooLarge {
18        /// Maximum allowed size.
19        max: usize,
20        /// Actual size.
21        actual: usize,
22    },
23
24    /// Execution result exceeds the configured maximum size.
25    #[error("output exceeds maximum size of {max} bytes")]
26    OutputTooLarge {
27        /// Maximum allowed size.
28        max: usize,
29    },
30
31    /// Execution timed out (async event loop or CPU-bound watchdog).
32    #[error("execution timed out after {timeout_ms}ms")]
33    Timeout {
34        /// Configured timeout in milliseconds.
35        timeout_ms: u64,
36    },
37
38    /// A banned code pattern was detected during validation.
39    #[error("banned pattern detected: `{pattern}` — the sandbox has no filesystem, network, or module access. Use forge.callTool() or forge.server() to interact with external services.")]
40    BannedPattern {
41        /// The pattern that was matched.
42        pattern: String,
43    },
44
45    /// Generic execution failure.
46    #[error("sandbox execution failed: {0}")]
47    Execution(#[from] anyhow::Error),
48
49    /// A JavaScript error was thrown during execution.
50    #[error("javascript error: {message}")]
51    JsError {
52        /// The error message from JavaScript.
53        message: String,
54    },
55
56    /// Result serialization failed.
57    #[error("result serialization failed: {0}")]
58    Serialization(#[from] serde_json::Error),
59
60    /// Too many concurrent sandbox executions.
61    #[error("concurrency limit reached (max {max} concurrent executions)")]
62    ConcurrencyLimit {
63        /// Maximum allowed concurrent executions.
64        max: usize,
65    },
66
67    /// Too many tool calls in a single execution.
68    #[error("tool call limit exceeded (max {max} calls per execution)")]
69    ToolCallLimit {
70        /// Maximum allowed tool calls.
71        max: usize,
72    },
73
74    /// Tool call arguments exceed the configured maximum size.
75    #[error("tool call arguments too large (max {max} bytes, got {actual})")]
76    ToolCallArgsTooLarge {
77        /// Maximum allowed argument size.
78        max: usize,
79        /// Actual argument size.
80        actual: usize,
81    },
82
83    /// V8 heap memory limit was exceeded.
84    #[error("V8 heap limit exceeded")]
85    HeapLimitExceeded,
86}