generated from famedly/rust-library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add reqwest response err helper
- Loading branch information
Showing
4 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::{fmt, future::Future}; | ||
|
||
/// Wrapper around [reqwest::Error] with optional response body | ||
#[derive(Debug, thiserror::Error)] | ||
pub struct ReqwestErrorWithBody { | ||
/// Error from [reqwest] | ||
pub error: reqwest::Error, | ||
/// Optional response body | ||
pub body: Option<String>, | ||
} | ||
|
||
impl From<reqwest::Error> for ReqwestErrorWithBody { | ||
fn from(error: reqwest::Error) -> ReqwestErrorWithBody { | ||
Self { error, body: None } | ||
} | ||
} | ||
|
||
impl fmt::Display for ReqwestErrorWithBody { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{} with body {}", self.error, self.body.as_deref().unwrap_or("<no body>")) | ||
} | ||
} | ||
|
||
/// An alternative to [reqwest::Resnpose::error_for_status] that also returns | ||
/// optional response body | ||
/// ```no_run | ||
/// # use famedly_rust_utils::reqwest::*; | ||
/// # async fn mk_req() -> Result<(), ReqwestErrorWithBody> { | ||
/// reqwest::get("http://invalid.example") | ||
/// .await? | ||
/// .error_for_status_with_body() | ||
/// .await?; | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
pub trait ErrorForStatusWithBody { | ||
// Using explicit `impl Future` syntax here instead of `async_fn_in_trait` to | ||
// make it `Send` | ||
#[allow(missing_docs)] | ||
fn error_for_status_with_body( | ||
self, | ||
) -> impl Future<Output = Result<reqwest::Response, ReqwestErrorWithBody>> + Send; | ||
} | ||
|
||
impl ErrorForStatusWithBody for reqwest::Response { | ||
#[allow(clippy::manual_async_fn)] | ||
fn error_for_status_with_body( | ||
self, | ||
) -> impl Future<Output = Result<reqwest::Response, ReqwestErrorWithBody>> + Send { | ||
async { | ||
if let Err(error) = self.error_for_status_ref() { | ||
Err(ReqwestErrorWithBody { error, body: self.text().await.ok() }) | ||
} else { | ||
Ok(self) | ||
} | ||
} | ||
} | ||
} |