Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions src/api/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,12 @@ pub(super) struct ProjectRenamedError(pub(super) String);
pub(super) type ApiResult<T> = Result<T, ApiError>;

#[derive(Debug, thiserror::Error)]
#[error("request failed with retryable status code {}", .body.status)]
pub(super) struct RetryError {
body: ApiResponse,
}

impl RetryError {
pub fn new(body: ApiResponse) -> Self {
Self { body }
}

pub fn into_body(self) -> ApiResponse {
self.body
}
pub(super) enum RetryError {
#[error("request failed with retryable status code {}", body.status)]
Status { body: ApiResponse },
#[error("request failed with retryable error: {source}")]
ApiError {
#[from]
source: ApiError,
},
}
24 changes: 21 additions & 3 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod serialization;
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::HashMap;
use std::error::Error as _;
#[cfg(any(target_os = "macos", not(feature = "managed")))]
use std::fs::File;
use std::io::{self, Read as _, Write};
Expand Down Expand Up @@ -1313,11 +1314,27 @@ impl ApiRequest {
debug!("retry number {retry_number}, max retries: {max_retries}");
*retry_number += 1;

let mut rv = self.send_into(&mut out)?;
let mut rv = match self.send_into(&mut out) {
Ok(rv) => rv,
Err(err) => {
let is_retryable_dns = err
.source()
.and_then(|s| s.downcast_ref::<curl::Error>())
.is_some_and(|e| e.is_couldnt_resolve_host());

// Wrap DNS errors in a RetryError so they get retried
if is_retryable_dns {
anyhow::bail!(RetryError::from(err));
}

anyhow::bail!(err);
}
};

rv.body = Some(out);

if RETRY_STATUS_CODES.contains(&rv.status) {
anyhow::bail!(RetryError::new(rv));
anyhow::bail!(RetryError::Status { body: rv });
}

Ok(rv)
Expand All @@ -1336,7 +1353,8 @@ impl ApiRequest {
})
.call()
.or_else(|err| match err.downcast::<RetryError>() {
Ok(err) => Ok(err.into_body()),
Ok(RetryError::Status { body }) => Ok(body),
Ok(RetryError::ApiError { source }) => Err(source),
Err(err) => Err(ApiError::with_source(ApiErrorKind::RequestFailed, err)),
})
}
Expand Down