From cf48b29fd868bc517a38a40074c3bf0d6c22f283 Mon Sep 17 00:00:00 2001 From: kernelkind Date: Mon, 1 Sep 2025 13:38:36 -0400 Subject: [PATCH] make endpoint error into struct Signed-off-by: kernelkind --- crates/notedeck/src/error.rs | 13 ++++++++++++- crates/notedeck/src/zaps/networking.rs | 17 +++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/crates/notedeck/src/error.rs b/crates/notedeck/src/error.rs index 323077ef..b352b769 100644 --- a/crates/notedeck/src/error.rs +++ b/crates/notedeck/src/error.rs @@ -33,15 +33,26 @@ pub enum ZapError { #[error("invalid lud16")] InvalidLud16(String), #[error("invalid endpoint response")] - EndpointError(String), + EndpointError(EndpointError), #[error("bech encoding/decoding error")] Bech(String), #[error("serialization/deserialization problem")] Serialization(String), #[error("nwc error")] NWC(String), + #[error("ndb error")] + Ndb(String), } +impl ZapError { + pub fn endpoint_error(error: String) -> ZapError { + ZapError::EndpointError(EndpointError(error)) + } +} + +#[derive(Debug, Clone)] +pub struct EndpointError(pub String); + impl From for Error { fn from(s: String) -> Self { Error::Generic(s) diff --git a/crates/notedeck/src/zaps/networking.rs b/crates/notedeck/src/zaps/networking.rs index 1bb8a5be..928ee279 100644 --- a/crates/notedeck/src/zaps/networking.rs +++ b/crates/notedeck/src/zaps/networking.rs @@ -17,9 +17,9 @@ async fn fetch_pay_req_async(url: &Url) -> Result let (sender, promise) = Promise::new(); let on_done = move |response: Result| { - let handle = response.map_err(ZapError::EndpointError).and_then(|resp| { + let handle = response.map_err(ZapError::endpoint_error).and_then(|resp| { if !resp.ok { - return Err(ZapError::EndpointError(format!( + return Err(ZapError::endpoint_error(format!( "bad http response: {}", resp.status_text ))); @@ -181,7 +181,7 @@ fn convert_lnurl_to_endpoint_url(lnurl: &str) -> Result { String::from_utf8(data).map_err(|e| ZapError::Bech(format!("string conversion: {e}")))?; Url::parse(&url_str) - .map_err(|e| ZapError::EndpointError(format!("endpoint url from lnurl is invalid: {e}"))) + .map_err(|e| ZapError::endpoint_error(format!("endpoint url from lnurl is invalid: {e}"))) } async fn fetch_pay_req_from_lnurl_async(lnurl: &str) -> Result { @@ -204,8 +204,9 @@ async fn fetch_invoice_lnurl_async( //let recipient = Pubkey::from_hex(&pay_req.nostr_pubkey) //.map_err(|e| ZapError::EndpointError(format!("invalid pubkey hex from endpoint: {e}")))?; - let mut base_url = Url::parse(&pay_req.callback_url) - .map_err(|e| ZapError::EndpointError(format!("invalid callback url from endpoint: {e}")))?; + let mut base_url = Url::parse(&pay_req.callback_url).map_err(|e| { + ZapError::endpoint_error(format!("invalid callback url from endpoint: {e}")) + })?; let (query, noteid) = { let comment: &str = ""; @@ -240,9 +241,9 @@ async fn fetch_invoice(req: &Url) -> Result { let request = ehttp::Request::get(req); let (sender, promise) = Promise::new(); let on_done = move |response: Result| { - let handle = response.map_err(ZapError::EndpointError).and_then(|resp| { + let handle = response.map_err(ZapError::endpoint_error).and_then(|resp| { if !resp.ok { - return Err(ZapError::EndpointError(format!( + return Err(ZapError::endpoint_error(format!( "invalid http response: {}", resp.status_text ))); @@ -290,7 +291,7 @@ fn generate_endpoint_url(lud16: &str) -> Result { if use_http { "" } else { "s" } ); - Url::parse(&url_str).map_err(|e| ZapError::EndpointError(e.to_string())) + Url::parse(&url_str).map_err(|e| ZapError::endpoint_error(e.to_string())) } #[cfg(test)]