1use adk_core::{AdkError, ErrorCategory, ErrorComponent};
4
5#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
21#[non_exhaustive]
22pub enum DriverError {
23 #[error("invalid driver call: {reason}")]
25 Invalid {
26 reason: String,
28 },
29 #[error("refused: {reason}")]
32 Refused {
33 reason: String,
35 },
36 #[error("rate limited: {reason}; try again in {retry_after_secs} s")]
38 RateLimited {
39 reason: String,
41 retry_after_secs: u64,
43 },
44 #[error("{driver} does not support {operation}; use a driver that does")]
46 Unsupported {
47 driver: String,
49 operation: String,
51 },
52 #[error("not found: {what}")]
54 NotFound {
55 what: String,
57 },
58 #[error(
60 "{service} refused the driver's credentials: {reason}; check the secret the credential broker holds for it"
61 )]
62 Unauthorized {
63 service: String,
65 reason: String,
67 },
68 #[error("{service} is unavailable: {reason}")]
70 Unavailable {
71 service: String,
73 reason: String,
75 },
76 #[error("{service} answered with something unreadable: {reason}")]
78 Protocol {
79 service: String,
81 reason: String,
83 },
84 #[error("no recorded response for {request}; record one or use a live adapter")]
86 NoRecording {
87 request: String,
89 },
90}
91
92impl DriverError {
93 pub fn invalid(reason: impl Into<String>) -> Self {
95 Self::Invalid { reason: reason.into() }
96 }
97
98 pub fn refused(reason: impl Into<String>) -> Self {
100 Self::Refused { reason: reason.into() }
101 }
102
103 pub fn unavailable(service: impl Into<String>, reason: impl ToString) -> Self {
105 Self::Unavailable { service: service.into(), reason: reason.to_string() }
106 }
107
108 pub fn protocol(service: impl Into<String>, reason: impl ToString) -> Self {
110 Self::Protocol { service: service.into(), reason: reason.to_string() }
111 }
112
113 pub fn not_found(what: impl Into<String>) -> Self {
115 Self::NotFound { what: what.into() }
116 }
117
118 pub fn unsupported(driver: impl Into<String>, operation: impl Into<String>) -> Self {
120 Self::Unsupported { driver: driver.into(), operation: operation.into() }
121 }
122
123 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
165pub type Result<T> = std::result::Result<T, DriverError>;