Skip to main content

adk_drivers/
error.rs

1//! The drivers' error type (spec 3.5 §7.13).
2
3use adk_core::{AdkError, ErrorCategory, ErrorComponent};
4
5/// Why a driver call did not happen (spec 3.5 §7.13).
6///
7/// Converts into [`AdkError`] with a `drivers.*` code, so a tool call that a rule refused
8/// reaches the model as that rule's reason. Never carries a credential.
9///
10/// # Example
11///
12/// ```rust
13/// use adk_drivers::DriverError;
14///
15/// let error = DriverError::refused("the recipient unsubscribed");
16/// assert_eq!(error.code(), "drivers.refused");
17/// let adk: adk_core::AdkError = error.into();
18/// assert_eq!(adk.category, adk_core::ErrorCategory::Forbidden);
19/// ```
20#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
21#[non_exhaustive]
22pub enum DriverError {
23    /// The call's input is not valid for the contract.
24    #[error("invalid driver call: {reason}")]
25    Invalid {
26        /// What is wrong, and how to fix it.
27        reason: String,
28    },
29    /// A rule of the contract refused the call: an unsubscribed recipient, a missing
30    /// consent receipt, failing domain authentication, a new recipient in a reply.
31    #[error("refused: {reason}")]
32    Refused {
33        /// Which rule refused it.
34        reason: String,
35    },
36    /// Too many calls of this kind for now.
37    #[error("rate limited: {reason}; try again in {retry_after_secs} s")]
38    RateLimited {
39        /// What is limited.
40        reason: String,
41        /// How long to wait.
42        retry_after_secs: u64,
43    },
44    /// The driver does not offer this operation.
45    #[error("{driver} does not support {operation}; use a driver that does")]
46    Unsupported {
47        /// The driver.
48        driver: String,
49        /// The operation.
50        operation: String,
51    },
52    /// The thing the call names does not exist.
53    #[error("not found: {what}")]
54    NotFound {
55        /// What was not found.
56        what: String,
57    },
58    /// The service refused the driver's credentials.
59    #[error(
60        "{service} refused the driver's credentials: {reason}; check the secret the credential broker holds for it"
61    )]
62    Unauthorized {
63        /// The service.
64        service: String,
65        /// What it said.
66        reason: String,
67    },
68    /// The service could not be reached or failed.
69    #[error("{service} is unavailable: {reason}")]
70    Unavailable {
71        /// The service.
72        service: String,
73        /// What failed.
74        reason: String,
75    },
76    /// The service answered with something the driver could not read.
77    #[error("{service} answered with something unreadable: {reason}")]
78    Protocol {
79        /// The service.
80        service: String,
81        /// What was wrong.
82        reason: String,
83    },
84    /// A recorded-response driver has no recorded answer for the call.
85    #[error("no recorded response for {request}; record one or use a live adapter")]
86    NoRecording {
87        /// The call, as the recording keys it.
88        request: String,
89    },
90}
91
92impl DriverError {
93    /// An [`Invalid`](Self::Invalid) error.
94    pub fn invalid(reason: impl Into<String>) -> Self {
95        Self::Invalid { reason: reason.into() }
96    }
97
98    /// A [`Refused`](Self::Refused) error.
99    pub fn refused(reason: impl Into<String>) -> Self {
100        Self::Refused { reason: reason.into() }
101    }
102
103    /// An [`Unavailable`](Self::Unavailable) error.
104    pub fn unavailable(service: impl Into<String>, reason: impl ToString) -> Self {
105        Self::Unavailable { service: service.into(), reason: reason.to_string() }
106    }
107
108    /// A [`Protocol`](Self::Protocol) error.
109    pub fn protocol(service: impl Into<String>, reason: impl ToString) -> Self {
110        Self::Protocol { service: service.into(), reason: reason.to_string() }
111    }
112
113    /// A [`NotFound`](Self::NotFound) error.
114    pub fn not_found(what: impl Into<String>) -> Self {
115        Self::NotFound { what: what.into() }
116    }
117
118    /// An [`Unsupported`](Self::Unsupported) error.
119    pub fn unsupported(driver: impl Into<String>, operation: impl Into<String>) -> Self {
120        Self::Unsupported { driver: driver.into(), operation: operation.into() }
121    }
122
123    /// The error code: `drivers.invalid`, `.refused`, `.rate_limited`, `.unsupported`,
124    /// `.not_found`, `.unauthorized`, `.unavailable`, `.protocol` or `.no_recording`.
125    pub fn code(&self) -> &'static str {
126        match self {
127            Self::Invalid { .. } => "drivers.invalid",
128            Self::Refused { .. } => "drivers.refused",
129            Self::RateLimited { .. } => "drivers.rate_limited",
130            Self::Unsupported { .. } => "drivers.unsupported",
131            Self::NotFound { .. } => "drivers.not_found",
132            Self::Unauthorized { .. } => "drivers.unauthorized",
133            Self::Unavailable { .. } => "drivers.unavailable",
134            Self::Protocol { .. } => "drivers.protocol",
135            Self::NoRecording { .. } => "drivers.no_recording",
136        }
137    }
138
139    fn category(&self) -> ErrorCategory {
140        match self {
141            Self::Invalid { .. } => ErrorCategory::InvalidInput,
142            Self::Refused { .. } => ErrorCategory::Forbidden,
143            Self::RateLimited { .. } => ErrorCategory::RateLimited,
144            Self::Unsupported { .. } => ErrorCategory::Unsupported,
145            Self::NotFound { .. } => ErrorCategory::NotFound,
146            Self::Unauthorized { .. } => ErrorCategory::Unauthorized,
147            Self::Unavailable { .. } => ErrorCategory::Unavailable,
148            Self::Protocol { .. } | Self::NoRecording { .. } => ErrorCategory::Internal,
149        }
150    }
151}
152
153impl From<DriverError> for AdkError {
154    fn from(error: DriverError) -> Self {
155        AdkError::new(ErrorComponent::Tool, error.category(), error.code(), error.to_string())
156    }
157}
158
159impl From<DriverError> for adk_kernel::KernelError {
160    fn from(error: DriverError) -> Self {
161        Self::Core(error.into())
162    }
163}
164
165/// The result of a driver call.
166pub type Result<T> = std::result::Result<T, DriverError>;