use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum CoreError {
#[error("invalid message: {0}")]
InvalidMessage(String),
#[error("conflict: expected version {expected}, found {actual}")]
Conflict { expected: u64, actual: u64 },
#[error("vector dimension mismatch: expected {expected}, got {actual}")]
DimensionMismatch { expected: usize, actual: usize },
#[error("not found: {0}")]
NotFound(String),
#[error("store error: {0}")]
Store(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProviderErrorKind {
RateLimited,
Auth,
BadRequest,
Server,
Network,
Stalled,
Decode,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, thiserror::Error)]
#[error("{kind:?}: {message}")]
pub struct ProviderError {
pub kind: ProviderErrorKind,
pub message: String,
pub retry_after_ms: Option<u64>,
}
impl ProviderError {
pub fn new(kind: ProviderErrorKind, message: impl Into<String>) -> Self {
Self { kind, message: message.into(), retry_after_ms: None }
}
pub fn with_retry_after(mut self, ms: u64) -> Self {
self.retry_after_ms = Some(ms);
self
}
pub fn is_retryable(&self) -> bool {
matches!(
self.kind,
ProviderErrorKind::RateLimited | ProviderErrorKind::Server | ProviderErrorKind::Network | ProviderErrorKind::Stalled
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn retryable_kinds() {
for k in [ProviderErrorKind::RateLimited, ProviderErrorKind::Server, ProviderErrorKind::Network, ProviderErrorKind::Stalled] {
assert!(ProviderError::new(k, "x").is_retryable());
}
for k in [ProviderErrorKind::Auth, ProviderErrorKind::BadRequest, ProviderErrorKind::Decode] {
assert!(!ProviderError::new(k, "x").is_retryable());
}
}
#[test]
fn carries_retry_after() {
let e = ProviderError::new(ProviderErrorKind::RateLimited, "slow down").with_retry_after(1500);
assert_eq!(e.retry_after_ms, Some(1500));
assert_eq!(e.to_string(), "RateLimited: slow down");
}
}