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: impl Into<String>) -> Self {
44        Self::InvalidInput(s.into())
45    }
46
47    /// Create an internal error.
48    pub fn internal(s: impl Into<String>) -> Self {
49        Self::Internal(s.into())
50    }
51
52    /// Create an external service error.
53    pub fn external(s: impl Into<String>) -> Self {
54        Self::External(s.into())
55    }
56
57    /// Create a not found error.
58    pub fn not_found(s: impl Into<String>) -> Self {
59        Self::NotFound(s.into())
60    }
61
62    /// Create a permission denied error.
63    pub fn permission(s: impl Into<String>) -> Self {
64        Self::Permission(s.into())
65    }
66
67    /// Create a cancelled error.
68    pub fn cancelled(reason: Option<String>) -> Self {
69        Self::Cancelled { reason }
70    }
71}