Better WrappedReqwestError docs (#9251)

Document the hack with which we insert a hint message for offline cases.
This commit is contained in:
konsti 2024-11-19 22:51:58 +01:00 committed by GitHub
parent c188b11b0a
commit 0913382aa5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 4 deletions

View File

@ -322,8 +322,10 @@ impl Deref for WrappedReqwestError {
impl Display for WrappedReqwestError { impl Display for WrappedReqwestError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.is_likely_offline() { if self.is_likely_offline() {
// Insert an extra hint, we'll show the wrapped error through `source`
f.write_str("Could not connect, are you offline?") f.write_str("Could not connect, are you offline?")
} else { } else {
// Show the wrapped error
Display::fmt(&self.0, f) Display::fmt(&self.0, f)
} }
} }
@ -332,11 +334,10 @@ impl Display for WrappedReqwestError {
impl std::error::Error for WrappedReqwestError { impl std::error::Error for WrappedReqwestError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if self.is_likely_offline() { if self.is_likely_offline() {
match &self.0 { // `Display` is inserting an extra message, so we need to show the wrapped error
reqwest_middleware::Error::Middleware(err) => Some(err.as_ref()), Some(&self.0)
reqwest_middleware::Error::Reqwest(err) => Some(err),
}
} else { } else {
// `Display` is showing the wrapped error, continue with its source
self.0.source() self.0.source()
} }
} }