1use serde_json::Value;
8
9pub type Result<T> = std::result::Result<T, Error>;
10
11#[derive(Debug, thiserror::Error)]
12pub enum Error {
13 #[error("GitFox authentication is required")]
15 AuthRequired,
16
17 #[error("GitFox authentication failed")]
19 AuthFailed,
20
21 #[error("{kind} not found: {reference}")]
23 NotFound {
24 kind: &'static str,
25 reference: String,
26 },
27
28 #[error("request timed out after {0}s")]
30 Timeout(u64),
31
32 #[error("network error: {0}")]
34 Network(String),
35
36 #[error("rate limited by the server: {message}")]
38 RateLimited {
39 retry_after: Option<u64>,
40 message: String,
41 },
42
43 #[error("API error (HTTP {status}): {message}")]
45 Api {
46 status: u16,
47 message: String,
48 body: Option<Value>,
49 },
50
51 #[error("invalid GitFox host URL: {0}")]
53 InvalidUrl(String),
54
55 #[error("could not decode response: {0}")]
57 Decode(String),
58
59 #[error("could not build HTTP client: {0}")]
61 Builder(String),
62}
63
64impl Error {
65 pub(crate) fn message_from_body(body: &Option<Value>, fallback: &str) -> String {
67 let Some(value) = body else {
68 return fallback.to_string();
69 };
70 for key in ["message", "error", "detail"] {
71 if let Some(s) = value.get(key).and_then(Value::as_str)
72 && !s.is_empty()
73 {
74 return s.to_string();
75 }
76 }
77 fallback.to_string()
78 }
79}