Skip to main content

machi_tools/
error.rs

1//! Tool-domain errors.
2
3use machi_types::{ErrorCode, MachiError, RetryClass};
4
5/// Tool error alias mapped into [`MachiError`].
6pub type ToolError = MachiError;
7
8/// Helpers for constructing tool errors.
9pub mod codes {
10    use super::{ErrorCode, MachiError, RetryClass};
11
12    /// Tool missing from registry.
13    #[must_use]
14    pub fn not_found(name: &str) -> MachiError {
15        MachiError::new(ErrorCode::ToolNotFound, format!("tool not found: {name}"))
16    }
17
18    /// Invalid arguments.
19    #[must_use]
20    pub fn invalid_args(msg: impl Into<String>) -> MachiError {
21        MachiError::new(ErrorCode::ToolInvalidArgs, msg)
22    }
23
24    /// Execution failure.
25    #[must_use]
26    pub fn execution(msg: impl Into<String>) -> MachiError {
27        MachiError::new(ErrorCode::ToolExecution, msg)
28    }
29
30    /// Timeout.
31    #[must_use]
32    pub fn timeout(msg: impl Into<String>) -> MachiError {
33        MachiError::new(ErrorCode::ToolTimeout, msg).with_retry(RetryClass::Backoff)
34    }
35
36    /// Cancelled.
37    #[must_use]
38    pub fn cancelled() -> MachiError {
39        MachiError::new(ErrorCode::ToolCancelled, "tool call cancelled")
40    }
41
42    /// Policy deny.
43    #[must_use]
44    pub fn denied(msg: impl Into<String>) -> MachiError {
45        MachiError::new(ErrorCode::ToolDenied, msg)
46    }
47
48    /// Approval gate rejected the call.
49    #[must_use]
50    pub fn approval_denied(msg: impl Into<String>) -> MachiError {
51        MachiError::new(ErrorCode::ToolApprovalDenied, msg)
52    }
53
54    /// Stream protocol violation.
55    #[must_use]
56    pub fn stream_protocol(msg: impl Into<String>) -> MachiError {
57        MachiError::new(ErrorCode::ToolStreamProtocol, msg)
58    }
59
60    /// Rate limited by upstream.
61    #[must_use]
62    pub fn rate_limited(msg: impl Into<String>) -> MachiError {
63        MachiError::new(ErrorCode::ToolRateLimited, msg).with_retry(RetryClass::Backoff)
64    }
65
66    /// Concurrency limit.
67    #[must_use]
68    pub fn concurrency_limit(msg: impl Into<String>) -> MachiError {
69        MachiError::new(ErrorCode::ToolConcurrencyLimit, msg)
70    }
71
72    /// Network failure.
73    #[must_use]
74    pub fn network(msg: impl Into<String>) -> MachiError {
75        MachiError::new(ErrorCode::ToolNetwork, msg).with_retry(RetryClass::Backoff)
76    }
77
78    /// Service unavailable.
79    #[must_use]
80    pub fn service_unavailable(msg: impl Into<String>) -> MachiError {
81        MachiError::new(ErrorCode::ToolServiceUnavailable, msg).with_retry(RetryClass::Backoff)
82    }
83}