use std::fmt; #[derive(Debug, thiserror::Error)] pub enum Error { #[error("io: {0}")] Io(#[from] std::io::Error), #[error("json: {0}")] Json(#[from] serde_json::Error), #[error("sqlite: {0}")] Db(String), #[error("sqlite: {0}")] Sqlite(#[from] rusqlite::Error), #[error("git: {0}")] Git(String), #[error("http fetch: {0}")] Http(String), #[error("child process {cmd}: {detail}")] Subprocess { cmd: String, detail: String }, #[error("assertion failed: {0}")] FailClosed(String), #[error("not found: {0}")] NotFound(String), #[error("conflict: {0}")] Conflict(String), #[error("invalid: {0}")] Invalid(String), #[error("blocked: {0}")] Blocked(String), #[error("sandbox pool exhausted: {0}")] PoolExhausted(String), #[error("world mismatch: {0}")] WorldMismatch(String), #[error("transient: {0}")] Transient(String), #[error("{0}")] Other(String), } impl Error { pub fn db(s: impl fmt::Display) -> Self { Error::Db(s.to_string()) } pub fn git(s: impl fmt::Display) -> Self { Error::Git(s.to_string()) } pub fn fail_closed(s: impl fmt::Display) -> Self { Error::FailClosed(s.to_string()) } pub fn not_found(s: impl fmt::Display) -> Self { Error::NotFound(s.to_string()) } pub fn conflict(s: impl fmt::Display) -> Self { Error::Conflict(s.to_string()) } pub fn invalid(s: impl fmt::Display) -> Self { Error::Invalid(s.to_string()) } pub fn blocked(s: impl fmt::Display) -> Self { Error::Blocked(s.to_string()) } pub fn other(s: impl fmt::Display) -> Self { Error::Other(s.to_string()) } pub fn transient(s: impl fmt::Display) -> Self { Error::Transient(s.to_string()) } pub fn pool_exhausted(s: impl fmt::Display) -> Self { Error::PoolExhausted(s.to_string()) } pub fn world_mismatch(s: impl fmt::Display) -> Self { Error::WorldMismatch(s.to_string()) } pub fn is_pool_exhausted(&self) -> bool { matches!(self, Error::PoolExhausted(_)) } pub fn is_transient(&self) -> bool { match self { Error::Http(_) | Error::Transient(_) => true, Error::PoolExhausted(_) | Error::WorldMismatch(_) => false, Error::Io(e) => matches!( e.kind(), std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut | std::io::ErrorKind::Interrupted | std::io::ErrorKind::BrokenPipe | std::io::ErrorKind::ConnectionReset | std::io::ErrorKind::ConnectionRefused | std::io::ErrorKind::ConnectionAborted ), _ => false, } } } pub type Result = std::result::Result; #[cfg(test)] mod tests { use super::*; #[test] fn a_full_disk_is_not_a_retryable_failure() { let enospc = Error::Io(std::io::Error::from_raw_os_error(28)); let eacces = Error::Io(std::io::Error::from_raw_os_error(13)); assert!(!enospc.is_transient(), "a full disk does not clear itself between retries"); assert!(!eacces.is_transient(), "a permissions problem does not clear itself either"); } #[test] fn what_a_moment_might_fix_is_retried() { let refused = Error::Io(std::io::Error::from_raw_os_error(111)); assert!(refused.is_transient()); assert!(Error::transient("mirror returned 502").is_transient()); assert!(Error::Http("connection reset".into()).is_transient()); assert!(!Error::invalid("recipe has no pkgbase").is_transient()); assert!(!Error::fail_closed("identity hash moved").is_transient()); assert!(!Error::pool_exhausted("at quota").is_transient()); assert!(!Error::world_mismatch("missing pinned dep").is_transient()); assert!(Error::pool_exhausted("at quota").is_pool_exhausted()); } }