From 96be5818a71f479867de2bbd0029a3cfb627fbe0 Mon Sep 17 00:00:00 2001 From: nazeh Date: Fri, 20 Dec 2024 13:50:59 +0300 Subject: [PATCH] feat(pubky): add handle_http_error! macro --- pubky/src/shared/auth.rs | 36 +++++++++++++++++++------------- pubky/src/shared/list_builder.rs | 4 ++-- pubky/src/shared/mod.rs | 12 +++++++++++ 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/pubky/src/shared/auth.rs b/pubky/src/shared/auth.rs index 52e8a1b..6bca924 100644 --- a/pubky/src/shared/auth.rs +++ b/pubky/src/shared/auth.rs @@ -14,7 +14,7 @@ use pubky_common::{ use anyhow::Result; -use crate::Client; +use crate::{handle_http_error, Client}; impl Client { /// Signup to a homeserver and update Pkarr accordingly. @@ -31,8 +31,9 @@ impl Client { .await .body(AuthToken::sign(keypair, vec![Capability::root()]).serialize()) .send() - .await? - .error_for_status()?; + .await?; + + handle_http_error!(response); self.publish_homeserver(keypair, &homeserver.to_string()) .await?; @@ -52,30 +53,32 @@ impl Client { /// Returns None if not signed in, or [reqwest::Error] /// if the response has any other `>=404` status code. pub(crate) async fn inner_session(&self, pubky: &PublicKey) -> Result> { - let res = self + let response = self .inner_request(Method::GET, format!("pubky://{}/session", pubky)) .await .send() .await?; - if res.status() == StatusCode::NOT_FOUND { + if response.status() == StatusCode::NOT_FOUND { return Ok(None); } - res.error_for_status_ref()?; + handle_http_error!(response); - let bytes = res.bytes().await?; + let bytes = response.bytes().await?; Ok(Some(Session::deserialize(&bytes)?)) } /// Signout from a homeserver. pub(crate) async fn inner_signout(&self, pubky: &PublicKey) -> Result<()> { - self.inner_request(Method::DELETE, format!("pubky://{}/session", pubky)) + let response = self + .inner_request(Method::DELETE, format!("pubky://{}/session", pubky)) .await .send() - .await? - .error_for_status()?; + .await?; + + handle_http_error!(response); #[cfg(not(target_arch = "wasm32"))] self.cookie_store.delete_session_after_signout(pubky); @@ -144,12 +147,14 @@ impl Client { path_segments.push(&channel_id); drop(path_segments); - self.inner_request(Method::POST, callback_url) + let response = self + .inner_request(Method::POST, callback_url) .await .body(encrypted_token) .send() - .await? - .error_for_status()?; + .await?; + + handle_http_error!(response); Ok(()) } @@ -160,8 +165,9 @@ impl Client { .await .body(token.serialize()) .send() - .await? - .error_for_status()?; + .await?; + + handle_http_error!(response); let bytes = response.bytes().await?; diff --git a/pubky/src/shared/list_builder.rs b/pubky/src/shared/list_builder.rs index 33f5168..cc31860 100644 --- a/pubky/src/shared/list_builder.rs +++ b/pubky/src/shared/list_builder.rs @@ -2,7 +2,7 @@ use reqwest::{IntoUrl, Method}; use anyhow::Result; -use crate::Client; +use crate::{handle_http_error, Client}; /// Helper struct to edit Pubky homeserver's list API options before sending them. #[derive(Debug)] @@ -99,7 +99,7 @@ impl<'a> ListBuilder<'a> { .send() .await?; - response.error_for_status_ref()?; + handle_http_error!(response); // TODO: bail on too large files. let bytes = response.bytes().await?; diff --git a/pubky/src/shared/mod.rs b/pubky/src/shared/mod.rs index 67b456f..63ca268 100644 --- a/pubky/src/shared/mod.rs +++ b/pubky/src/shared/mod.rs @@ -2,3 +2,15 @@ pub mod auth; pub mod list_builder; pub mod pkarr; pub mod public; + +#[macro_export] +macro_rules! handle_http_error { + ($res:expr) => { + if let Err(status) = $res.error_for_status_ref() { + return match $res.text().await { + Ok(text) => Err(anyhow::anyhow!("{status}. Error message: {text}")), + _ => Err(anyhow::anyhow!("{status}")), + }; + } + }; +}