1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[non_exhaustive]
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
6#[serde(rename_all = "snake_case")]
7pub enum ErrorCode {
8 Invalid,
9 Unauthorized,
10 PermissionDenied,
11 NotFound,
12 Conflict,
13 PayloadTooLarge,
14 LimitExceeded,
15 BudgetExceeded,
16 Unavailable,
17 Internal,
18 Unknown,
19}
20
21impl ErrorCode {
22 pub fn as_str(self) -> &'static str {
23 match self {
24 Self::Invalid => "invalid",
25 Self::Unauthorized => "unauthorized",
26 Self::PermissionDenied => "permission_denied",
27 Self::NotFound => "not_found",
28 Self::Conflict => "conflict",
29 Self::PayloadTooLarge => "payload_too_large",
30 Self::LimitExceeded => "limit_exceeded",
31 Self::BudgetExceeded => "budget_exceeded",
32 Self::Unavailable => "unavailable",
33 Self::Internal => "internal",
34 Self::Unknown => "unknown",
35 }
36 }
37
38 pub fn parse(s: &str) -> Self {
39 match s {
40 "invalid" => Self::Invalid,
41 "unauthorized" => Self::Unauthorized,
42 "permission_denied" => Self::PermissionDenied,
43 "not_found" => Self::NotFound,
44 "conflict" => Self::Conflict,
45 "payload_too_large" => Self::PayloadTooLarge,
46 "limit_exceeded" => Self::LimitExceeded,
47 "budget_exceeded" => Self::BudgetExceeded,
48 "unavailable" => Self::Unavailable,
49 "internal" => Self::Internal,
50 _ => Self::Unknown,
51 }
52 }
53
54 pub fn is_retryable(self) -> bool {
55 matches!(self, Self::Unavailable)
56 }
57}
58
59#[non_exhaustive]
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
61#[serde(rename_all = "snake_case")]
62pub enum UnavailableReason {
63 Timeout,
64 Unreachable,
65 NotReady,
66 InvalidResponse,
67}
68
69#[non_exhaustive]
70#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
71#[serde(rename_all = "camelCase")]
72pub struct Head {
73 pub revision_id: String,
74 pub version: String,
75}
76
77#[non_exhaustive]
78#[derive(Debug, thiserror::Error)]
79pub enum ConfigError {
80 #[error("endpoint must be an HTTP(S) origin without credentials, query, fragment, or path")]
81 InvalidEndpoint,
82 #[error("failed to read token file: {0}")]
83 TokenFile(#[from] std::io::Error),
84 #[error("failed to build HTTP client: {0}")]
85 Http(String),
86}
87
88#[non_exhaustive]
89#[derive(Debug, thiserror::Error)]
90pub enum KeepError {
91 #[error("keep api error ({code:?}): {message}")]
92 Api {
93 status: u16,
94 code: ErrorCode,
95 message: String,
96 reason: Option<String>,
97 details: Option<Value>,
98 },
99 #[error("keep unavailable ({reason:?}): {message}")]
100 Unavailable {
101 reason: UnavailableReason,
102 status: Option<u16>,
103 message: String,
104 },
105}
106
107impl KeepError {
108 pub fn code(&self) -> Option<ErrorCode> {
109 match self {
110 Self::Api { code, .. } => Some(*code),
111 Self::Unavailable { .. } => None,
112 }
113 }
114
115 pub fn reason(&self) -> Option<&str> {
116 match self {
117 Self::Api { reason, .. } => reason.as_deref(),
118 Self::Unavailable { .. } => None,
119 }
120 }
121
122 pub fn unavailable_reason(&self) -> Option<UnavailableReason> {
123 match self {
124 Self::Unavailable { reason, .. } => Some(*reason),
125 Self::Api { .. } => None,
126 }
127 }
128
129 pub fn is_not_found(&self) -> bool {
130 matches!(
131 self,
132 Self::Api {
133 code: ErrorCode::NotFound,
134 ..
135 }
136 )
137 }
138
139 pub fn is_conflict(&self) -> bool {
140 matches!(
141 self,
142 Self::Api {
143 code: ErrorCode::Conflict,
144 ..
145 }
146 )
147 }
148
149 pub fn is_retryable(&self) -> bool {
150 match self {
151 Self::Api { code, .. } => code.is_retryable(),
152 Self::Unavailable { reason, .. } => {
153 matches!(
154 reason,
155 UnavailableReason::Unreachable | UnavailableReason::NotReady
156 )
157 }
158 }
159 }
160
161 pub fn conflict_head(&self) -> Option<Head> {
162 let Self::Api {
163 code: ErrorCode::Conflict,
164 details: Some(details),
165 ..
166 } = self
167 else {
168 return None;
169 };
170 let current = details.get("current")?;
171 serde_json::from_value(current.clone()).ok()
172 }
173}
174
175pub type Result<T> = std::result::Result<T, KeepError>;