Skip to main content

agentic_tools_core/
error.rs

1//! Unified error type for agentic tools.
2
3use thiserror::Error;
4
5/// Error type returned by tool operations.
6#[derive(Error, Debug)]
7pub enum ToolError {
8    /// Invalid input provided to the tool.
9    #[error("invalid input: {0}")]
10    InvalidInput(String),
11
12    /// Internal error during tool execution.
13    #[error("internal error: {0}")]
14    Internal(String),
15
16    /// Error from an external service.
17    #[error("external service error: {0}")]
18    External(String),
19
20    /// Permission denied for the operation.
21    #[error("permission denied: {0}")]
22    Permission(String),
23
24    /// Requested resource not found.
25    #[error("not found: {0}")]
26    NotFound(String),
27
28    /// Tool execution stopped because the caller cancelled the request.
29    #[error(
30        "cancelled{}",
31        .reason
32            .as_deref()
33            .map_or_else(String::new, |reason| format!(": {reason}"))
34    )]
35    Cancelled {
36        /// Optional detail about the cancellation reason.
37        reason: Option<String>,
38    },
39}
40
41impl ToolError {
42    /// Create an invalid input error.
43    pub fn invalid_input<S: ToString>(s: S) -> Self {
44        ToolError::InvalidInput(s.to_string())
45    }
46
47    /// Create an internal error.
48    pub fn internal<S: ToString>(s: S) -> Self {
49        ToolError::Internal(s.to_string())
50    }
51
52    /// Create an external service error.
53    pub fn external<S: ToString>(s: S) -> Self {
54        ToolError::External(s.to_string())
55    }
56
57    /// Create a not found error.
58    pub fn not_found<S: ToString>(s: S) -> Self {
59        ToolError::NotFound(s.to_string())
60    }
61
62    /// Create a permission denied error.
63    pub fn permission<S: ToString>(s: S) -> Self {
64        ToolError::Permission(s.to_string())
65    }
66
67    /// Create a cancelled error.
68    pub fn cancelled(reason: Option<String>) -> Self {
69        ToolError::Cancelled { reason }
70    }
71}