Skip to content

Commit 3552d53

Browse files
ref: Remove blanket expect from api (#2679)
The `api` module previously had a blanket `expect` for `unwrap_used`. Here, we replace the blanket expect with individual expects for each violation, or where simple, fix the violations
1 parent 052c798 commit 3552d53

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/api/mod.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![expect(clippy::unwrap_used, reason = "contains legacy code which uses unwrap")]
2-
31
//! This module implements the API access to the Sentry API as well
42
//! as some other APIs we interact with. In particular it can talk
53
//! to the GitHub API to figure out if there are new releases of the
@@ -160,6 +158,7 @@ impl Api {
160158
pub fn with_config(config: Arc<Config>) -> Api {
161159
Api {
162160
config,
161+
#[expect(clippy::unwrap_used, reason = "legacy code")]
163162
pool: r2d2::Pool::builder()
164163
.max_size(16)
165164
.build(CurlConnectionManager)
@@ -223,7 +222,11 @@ impl Api {
223222
url: &str,
224223
auth: Option<&Auth>,
225224
) -> ApiResult<ApiRequest> {
226-
let mut handle = self.pool.get().unwrap();
225+
let mut handle = self
226+
.pool
227+
.get()
228+
.map_err(|e| ApiError::with_source(ApiErrorKind::RequestFailed, e))?;
229+
227230
handle.reset();
228231
if !self.config.allow_keepalive() {
229232
handle.forbid_reuse(true).ok();
@@ -310,7 +313,11 @@ impl Api {
310313
}
311314
}
312315
}
313-
std::thread::sleep(Duration::milliseconds(500).to_std().unwrap());
316+
std::thread::sleep(
317+
Duration::milliseconds(500)
318+
.to_std()
319+
.expect("500ms is valid, as it is non-negative"),
320+
);
314321
if Utc::now() - duration > started {
315322
return Ok(false);
316323
}
@@ -1608,6 +1615,7 @@ fn handle_req<W: Write>(
16081615
})?;
16091616
} else if progress_bar_mode.active() {
16101617
let pb_progress = pb.clone();
1618+
#[expect(clippy::unwrap_used, reason = "legacy code")]
16111619
handle.progress_function(move |a, b, c, d| {
16121620
let (down_len, down_pos, up_len, up_pos) = (a as u64, b as u64, c as u64, d as u64);
16131621
let mut pb = pb_progress.borrow_mut();
@@ -1661,8 +1669,8 @@ fn handle_req<W: Write>(
16611669
handle.perform()?;
16621670
}
16631671

1664-
if pb.borrow().is_some() {
1665-
pb.borrow().as_ref().unwrap().finish_and_clear();
1672+
if let Some(pb) = pb.borrow().as_ref() {
1673+
pb.finish_and_clear();
16661674
}
16671675

16681676
Ok((handle.response_code()?, headers))
@@ -1812,6 +1820,7 @@ impl ApiRequest {
18121820
fn get_headers(&self) -> curl::easy::List {
18131821
let mut result = curl::easy::List::new();
18141822
for header_bytes in self.headers.iter() {
1823+
#[expect(clippy::unwrap_used, reason = "legacy code")]
18151824
let header = String::from_utf8(header_bytes.to_vec()).unwrap();
18161825
result.append(&header).ok();
18171826
}
@@ -1851,7 +1860,10 @@ impl ApiRequest {
18511860
}
18521861

18531862
// Exponential backoff
1854-
let backoff_timeout = backoff.next_backoff().unwrap();
1863+
let backoff_timeout = backoff
1864+
.next_backoff()
1865+
.expect("should not return None, as there is no max_elapsed_time");
1866+
18551867
debug!(
18561868
"retry number {}, retrying again in {} ms",
18571869
retry_number,
@@ -2004,7 +2016,8 @@ impl ApiResponse {
20042016

20052017
fn log_headers(is_response: bool, data: &[u8]) {
20062018
lazy_static! {
2007-
static ref AUTH_RE: Regex = Regex::new(r"(?i)(authorization):\s*([\w]+)\s+(.*)").unwrap();
2019+
static ref AUTH_RE: Regex =
2020+
Regex::new(r"(?i)(authorization):\s*([\w]+)\s+(.*)").expect("regex is valid");
20082021
}
20092022
if let Ok(header) = std::str::from_utf8(data) {
20102023
for line in header.lines() {
@@ -2014,6 +2027,7 @@ fn log_headers(is_response: bool, data: &[u8]) {
20142027

20152028
let replaced = AUTH_RE.replace_all(line, |caps: &Captures<'_>| {
20162029
let info = if &caps[1].to_lowercase() == "basic" {
2030+
#[expect(clippy::unwrap_used, reason = "legacy code")]
20172031
caps[3].split(':').next().unwrap().to_owned()
20182032
} else {
20192033
format!("{}***", &caps[3][..std::cmp::min(caps[3].len(), 8)])

0 commit comments

Comments
 (0)