Make accepted codes non-optional

This commit is contained in:
Thomas Zahner 2025-05-12 11:46:50 +02:00
parent d22d1888f1
commit 3100fb2ee7
5 changed files with 12 additions and 15 deletions

View file

@ -17,10 +17,7 @@ async fn main() -> Result<()> {
let mut headers = HeaderMap::new();
headers.insert(header::ACCEPT, "text/html".parse().unwrap());
let accepted = Some(HashSet::from_iter(vec![
StatusCode::OK,
StatusCode::NO_CONTENT,
]));
let accepted = HashSet::from_iter(vec![StatusCode::OK, StatusCode::NO_CONTENT]);
let client = ClientBuilder::builder()
.excludes(excludes)

View file

@ -36,7 +36,7 @@ pub(crate) struct WebsiteChecker {
/// Set of accepted return codes / status codes.
///
/// Unmatched return codes/ status codes are deemed as errors.
accepted: Option<HashSet<StatusCode>>,
accepted: HashSet<StatusCode>,
/// Requires using HTTPS when it's available.
///
@ -59,7 +59,7 @@ impl WebsiteChecker {
retry_wait_time: Duration,
max_retries: u64,
reqwest_client: reqwest::Client,
accepted: Option<HashSet<StatusCode>>,
accepted: HashSet<StatusCode>,
github_client: Option<Octocrab>,
require_https: bool,
plugin_request_chain: RequestChain,

View file

@ -238,7 +238,10 @@ pub struct ClientBuilder {
/// Set of accepted return codes / status codes.
///
/// Unmatched return codes/ status codes are deemed as errors.
accepted: Option<HashSet<StatusCode>>,
///
/// TODO: accept all "valid" status codes by default. Maybe use `AcceptRange`?
#[builder(default = HashSet::from([StatusCode::OK]))]
accepted: HashSet<StatusCode>,
/// Response timeout per request in seconds.
timeout: Option<Duration>,
@ -633,7 +636,7 @@ mod tests {
password: "pass".into(),
});
let res = get_mock_client_response(r).await;
let res = dbg!(get_mock_client_response(r).await);
assert!(res.status().is_success());
}

View file

@ -94,11 +94,6 @@ impl RetryExt for http::Error {
impl RetryExt for ErrorKind {
fn should_retry(&self) -> bool {
match self {
Self::RejectedStatusCode(StatusCode::TOO_MANY_REQUESTS) => return true,
_ => {}
};
// If the error is a `reqwest::Error`, delegate to that
if let Some(r) = self.reqwest_error() {
r.should_retry()
@ -110,6 +105,8 @@ impl RetryExt for ErrorKind {
}) = self.github_error()
{
source.should_retry()
} else if let Self::RejectedStatusCode(StatusCode::TOO_MANY_REQUESTS) = self {
return true;
} else {
false
}

View file

@ -83,10 +83,10 @@ impl Serialize for Status {
impl Status {
#[must_use]
/// Create a status object from a response and the set of accepted status codes
pub fn new(response: &Response, accepted: Option<HashSet<StatusCode>>) -> Self {
pub fn new(response: &Response, accepted: HashSet<StatusCode>) -> Self {
let code = response.status();
if let Some(true) = accepted.map(|a| a.contains(&code)) {
if accepted.contains(&code) {
Self::Ok(code)
} else {
Self::Error(ErrorKind::RejectedStatusCode(code))