use serde_json::Value;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("GitFox authentication is required")]
AuthRequired,
#[error("GitFox authentication failed")]
AuthFailed,
#[error("{kind} not found: {reference}")]
NotFound {
kind: &'static str,
reference: String,
},
#[error("request timed out after {0}s")]
Timeout(u64),
#[error("network error: {0}")]
Network(String),
#[error("rate limited by the server: {message}")]
RateLimited {
retry_after: Option<u64>,
message: String,
},
#[error("API error (HTTP {status}): {message}")]
Api {
status: u16,
message: String,
body: Option<Value>,
},
#[error("invalid GitFox host URL: {0}")]
InvalidUrl(String),
#[error("could not decode response: {0}")]
Decode(String),
#[error("could not build HTTP client: {0}")]
Builder(String),
}
impl Error {
pub(crate) fn message_from_body(body: &Option<Value>, fallback: &str) -> String {
let Some(value) = body else {
return fallback.to_string();
};
for key in ["message", "error", "detail"] {
if let Some(s) = value.get(key).and_then(Value::as_str)
&& !s.is_empty()
{
return s.to_string();
}
}
fallback.to_string()
}
}