use serde::{Deserialize, Serialize};
use serde_json::Value;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ErrorCode {
Invalid,
Unauthorized,
PermissionDenied,
NotFound,
Conflict,
PayloadTooLarge,
LimitExceeded,
BudgetExceeded,
Unavailable,
Internal,
Unknown,
}
impl ErrorCode {
pub fn as_str(self) -> &'static str {
match self {
Self::Invalid => "invalid",
Self::Unauthorized => "unauthorized",
Self::PermissionDenied => "permission_denied",
Self::NotFound => "not_found",
Self::Conflict => "conflict",
Self::PayloadTooLarge => "payload_too_large",
Self::LimitExceeded => "limit_exceeded",
Self::BudgetExceeded => "budget_exceeded",
Self::Unavailable => "unavailable",
Self::Internal => "internal",
Self::Unknown => "unknown",
}
}
pub fn parse(s: &str) -> Self {
match s {
"invalid" => Self::Invalid,
"unauthorized" => Self::Unauthorized,
"permission_denied" => Self::PermissionDenied,
"not_found" => Self::NotFound,
"conflict" => Self::Conflict,
"payload_too_large" => Self::PayloadTooLarge,
"limit_exceeded" => Self::LimitExceeded,
"budget_exceeded" => Self::BudgetExceeded,
"unavailable" => Self::Unavailable,
"internal" => Self::Internal,
_ => Self::Unknown,
}
}
pub fn is_retryable(self) -> bool {
matches!(self, Self::Unavailable)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum UnavailableReason {
Timeout,
Unreachable,
NotReady,
InvalidResponse,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Head {
pub revision_id: String,
pub version: String,
}
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
#[error("endpoint must be an HTTP(S) origin without credentials, query, fragment, or path")]
InvalidEndpoint,
#[error("failed to read token file: {0}")]
TokenFile(#[from] std::io::Error),
#[error("failed to build HTTP client: {0}")]
Http(String),
}
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum KeepError {
#[error("keep api error ({code:?}): {message}")]
Api {
status: u16,
code: ErrorCode,
message: String,
reason: Option<String>,
details: Option<Value>,
},
#[error("keep unavailable ({reason:?}): {message}")]
Unavailable {
reason: UnavailableReason,
status: Option<u16>,
message: String,
},
}
impl KeepError {
pub fn code(&self) -> Option<ErrorCode> {
match self {
Self::Api { code, .. } => Some(*code),
Self::Unavailable { .. } => None,
}
}
pub fn reason(&self) -> Option<&str> {
match self {
Self::Api { reason, .. } => reason.as_deref(),
Self::Unavailable { .. } => None,
}
}
pub fn unavailable_reason(&self) -> Option<UnavailableReason> {
match self {
Self::Unavailable { reason, .. } => Some(*reason),
Self::Api { .. } => None,
}
}
pub fn is_not_found(&self) -> bool {
matches!(
self,
Self::Api {
code: ErrorCode::NotFound,
..
}
)
}
pub fn is_conflict(&self) -> bool {
matches!(
self,
Self::Api {
code: ErrorCode::Conflict,
..
}
)
}
pub fn is_retryable(&self) -> bool {
match self {
Self::Api { code, .. } => code.is_retryable(),
Self::Unavailable { reason, .. } => {
matches!(
reason,
UnavailableReason::Unreachable | UnavailableReason::NotReady
)
}
}
}
pub fn conflict_head(&self) -> Option<Head> {
let Self::Api {
code: ErrorCode::Conflict,
details: Some(details),
..
} = self
else {
return None;
};
let current = details.get("current")?;
serde_json::from_value(current.clone()).ok()
}
}
pub type Result<T> = std::result::Result<T, KeepError>;