use adk_core::{AdkError, ErrorCategory, ErrorComponent};
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum DriverError {
#[error("invalid driver call: {reason}")]
Invalid {
reason: String,
},
#[error("refused: {reason}")]
Refused {
reason: String,
},
#[error("rate limited: {reason}; try again in {retry_after_secs} s")]
RateLimited {
reason: String,
retry_after_secs: u64,
},
#[error("{driver} does not support {operation}; use a driver that does")]
Unsupported {
driver: String,
operation: String,
},
#[error("not found: {what}")]
NotFound {
what: String,
},
#[error(
"{service} refused the driver's credentials: {reason}; check the secret the credential broker holds for it"
)]
Unauthorized {
service: String,
reason: String,
},
#[error("{service} is unavailable: {reason}")]
Unavailable {
service: String,
reason: String,
},
#[error("{service} answered with something unreadable: {reason}")]
Protocol {
service: String,
reason: String,
},
#[error("no recorded response for {request}; record one or use a live adapter")]
NoRecording {
request: String,
},
}
impl DriverError {
pub fn invalid(reason: impl Into<String>) -> Self {
Self::Invalid { reason: reason.into() }
}
pub fn refused(reason: impl Into<String>) -> Self {
Self::Refused { reason: reason.into() }
}
pub fn unavailable(service: impl Into<String>, reason: impl ToString) -> Self {
Self::Unavailable { service: service.into(), reason: reason.to_string() }
}
pub fn protocol(service: impl Into<String>, reason: impl ToString) -> Self {
Self::Protocol { service: service.into(), reason: reason.to_string() }
}
pub fn not_found(what: impl Into<String>) -> Self {
Self::NotFound { what: what.into() }
}
pub fn unsupported(driver: impl Into<String>, operation: impl Into<String>) -> Self {
Self::Unsupported { driver: driver.into(), operation: operation.into() }
}
pub fn code(&self) -> &'static str {
match self {
Self::Invalid { .. } => "drivers.invalid",
Self::Refused { .. } => "drivers.refused",
Self::RateLimited { .. } => "drivers.rate_limited",
Self::Unsupported { .. } => "drivers.unsupported",
Self::NotFound { .. } => "drivers.not_found",
Self::Unauthorized { .. } => "drivers.unauthorized",
Self::Unavailable { .. } => "drivers.unavailable",
Self::Protocol { .. } => "drivers.protocol",
Self::NoRecording { .. } => "drivers.no_recording",
}
}
fn category(&self) -> ErrorCategory {
match self {
Self::Invalid { .. } => ErrorCategory::InvalidInput,
Self::Refused { .. } => ErrorCategory::Forbidden,
Self::RateLimited { .. } => ErrorCategory::RateLimited,
Self::Unsupported { .. } => ErrorCategory::Unsupported,
Self::NotFound { .. } => ErrorCategory::NotFound,
Self::Unauthorized { .. } => ErrorCategory::Unauthorized,
Self::Unavailable { .. } => ErrorCategory::Unavailable,
Self::Protocol { .. } | Self::NoRecording { .. } => ErrorCategory::Internal,
}
}
}
impl From<DriverError> for AdkError {
fn from(error: DriverError) -> Self {
AdkError::new(ErrorComponent::Tool, error.category(), error.code(), error.to_string())
}
}
impl From<DriverError> for adk_kernel::KernelError {
fn from(error: DriverError) -> Self {
Self::Core(error.into())
}
}
pub type Result<T> = std::result::Result<T, DriverError>;