Skip to content

Commit 5f01168

Browse files
committed
extra logging and clearer errors
1 parent 2713cc7 commit 5f01168

2 files changed

Lines changed: 29 additions & 37 deletions

File tree

crates/uv-client/src/cached_client.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ use crate::{
2626
///
2727
/// Note: This consumes the response body, so it should only be called when there's an error status.
2828
async fn extract_problem_details(response: Response) -> Option<ProblemDetails> {
29-
if let Ok(bytes) = response.bytes().await {
30-
serde_json::from_slice(&bytes).ok()
31-
} else {
32-
warn!("Failed to read response body for problem details");
33-
None
29+
match response.bytes().await {
30+
Ok(bytes) => match serde_json::from_slice(&bytes) {
31+
Ok(details) => Some(details),
32+
Err(err) => {
33+
warn!("Failed to parse problem details: {err}");
34+
None
35+
}
36+
},
37+
Err(err) => {
38+
warn!("Failed to read response body for problem details: {err}");
39+
None
40+
}
3441
}
3542
}
3643

crates/uv-client/src/error.rs

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,25 @@ fn default_problem_type() -> String {
4444

4545
impl ProblemDetails {
4646
/// Get a human-readable description of the problem
47-
pub fn description(&self) -> String {
47+
pub fn description(&self) -> Option<String> {
4848
match self {
4949
Self {
5050
title: Some(title),
5151
detail: Some(detail),
5252
..
53-
} => {
54-
format!("{title}: {detail}")
55-
}
53+
} => Some(format!("Server message: {title}, {detail}")),
5654
Self {
5755
title: Some(title), ..
58-
} => title.clone(),
56+
} => Some(format!("Server message: {title}")),
5957
Self {
6058
detail: Some(detail),
6159
..
62-
} => detail.clone(),
60+
} => Some(format!("Server message: {detail}")),
6361
Self {
6462
status: Some(status),
6563
..
66-
} => {
67-
format!("HTTP error {status}")
68-
}
69-
_ => {
70-
// If no detail, title, or status is provided, return a generic message
71-
"An error occurred".to_string()
72-
}
64+
} => Some(format!("HTTP error {status}")),
65+
_ => None,
7366
}
7467
}
7568
}
@@ -432,20 +425,6 @@ impl WrappedReqwestError {
432425
}
433426
}
434427

435-
/// Get the problem details if available
436-
pub fn problem_details(&self) -> Option<&ProblemDetails> {
437-
self.problem_details.as_ref()
438-
}
439-
440-
/// Get a user-friendly error message, preferring problem details if available
441-
pub fn user_message(&self) -> String {
442-
if let Some(problem_details) = &self.problem_details {
443-
problem_details.description()
444-
} else {
445-
self.error.to_string()
446-
}
447-
}
448-
449428
/// Return the inner [`reqwest::Error`] from the error chain, if it exists.
450429
fn inner(&self) -> Option<&reqwest::Error> {
451430
match &self.error {
@@ -541,7 +520,10 @@ impl Display for WrappedReqwestError {
541520
f.write_str("Could not connect, are you offline?")
542521
} else if let Some(problem_details) = &self.problem_details {
543522
// Show problem details if available
544-
write!(f, "{}", problem_details.description())
523+
match problem_details.description() {
524+
None => Display::fmt(&self.error, f),
525+
Some(message) => f.write_str(&message),
526+
}
545527
} else {
546528
// Show the wrapped error
547529
Display::fmt(&self.error, f)
@@ -624,8 +606,8 @@ mod tests {
624606

625607
let problem_details: ProblemDetails = serde_json::from_slice(json.as_bytes()).unwrap();
626608
assert_eq!(
627-
problem_details.description(),
628-
"Error Title: Detailed error message"
609+
problem_details.description().unwrap(),
610+
"Server message: Error Title, Detailed error message"
629611
);
630612

631613
let json_no_detail = r#"{
@@ -635,15 +617,18 @@ mod tests {
635617

636618
let problem_details: ProblemDetails =
637619
serde_json::from_slice(json_no_detail.as_bytes()).unwrap();
638-
assert_eq!(problem_details.description(), "Error Title");
620+
assert_eq!(
621+
problem_details.description().unwrap(),
622+
"Server message: Error Title"
623+
);
639624

640625
let json_minimal = r#"{
641626
"status": 400
642627
}"#;
643628

644629
let problem_details: ProblemDetails =
645630
serde_json::from_slice(json_minimal.as_bytes()).unwrap();
646-
assert_eq!(problem_details.description(), "HTTP error 400");
631+
assert_eq!(problem_details.description().unwrap(), "HTTP error 400");
647632
}
648633

649634
#[test]

0 commit comments

Comments
 (0)