pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("fetch: {0}")]
Fetch(String),
#[error("http status {status} for {url}")]
HttpStatus {
status: u16,
url: String,
},
#[error("redirect from {url} exceeds the {hops}-redirect limit")]
RedirectLimit {
url: String,
hops: u32,
},
#[error("fetched object exceeds the {limit}-byte cap")]
FetchTooLarge {
limit: u64,
},
#[error("response for {url} carries the coding {encoding}")]
ContentEncoded {
url: String,
encoding: String,
},
#[error("unsupported: {0}")]
Unsupported(String),
#[error("upload to {url} interrupted: {message}")]
UploadInterrupted {
url: String,
message: String,
},
}
impl Error {
pub fn is_unsent(&self) -> bool {
!matches!(self, Error::UploadInterrupted { .. })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_interrupted_upload_alone_was_sent() {
let interrupted = Error::UploadInterrupted {
url: "https://h/x".into(),
message: "response for https://h/x carries the coding gzip".into(),
};
assert!(!interrupted.is_unsent());
for unsent in [
Error::Fetch("connect to h:443 failed".into()),
Error::Unsupported("fetch url scheme ftp".into()),
Error::HttpStatus {
status: 407,
url: "https://h/x".into(),
},
] {
assert!(unsent.is_unsent(), "{unsent}");
}
}
}