diff --git a/payjoin/src/send/error.rs b/payjoin/src/send/error.rs index 0b633016..4f9ca6a3 100644 --- a/payjoin/src/send/error.rs +++ b/payjoin/src/send/error.rs @@ -90,64 +90,54 @@ impl std::error::Error for BuildSenderError { /// `unwrap()`ing it is thus considered OK in Rust but you may achieve nicer message by displaying /// it. #[derive(Debug)] +#[cfg(feature = "v2")] pub struct CreateRequestError(InternalCreateRequestError); #[derive(Debug)] +#[cfg(feature = "v2")] pub(crate) enum InternalCreateRequestError { Url(url::ParseError), - #[cfg(feature = "v2")] Hpke(crate::hpke::HpkeError), - #[cfg(feature = "v2")] OhttpEncapsulation(crate::ohttp::OhttpEncapsulationError), - #[cfg(feature = "v2")] ParseReceiverPubkey(ParseReceiverPubkeyParamError), - #[cfg(feature = "v2")] MissingOhttpConfig, - #[cfg(feature = "v2")] Expired(std::time::SystemTime), } +#[cfg(feature = "v2")] impl fmt::Display for CreateRequestError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use InternalCreateRequestError::*; match &self.0 { Url(e) => write!(f, "cannot parse url: {:#?}", e), - #[cfg(feature = "v2")] Hpke(e) => write!(f, "v2 error: {}", e), - #[cfg(feature = "v2")] OhttpEncapsulation(e) => write!(f, "v2 error: {}", e), - #[cfg(feature = "v2")] ParseReceiverPubkey(e) => write!(f, "cannot parse receiver public key: {}", e), - #[cfg(feature = "v2")] MissingOhttpConfig => write!(f, "no ohttp configuration with which to make a v2 request available"), - #[cfg(feature = "v2")] Expired(expiry) => write!(f, "session expired at {:?}", expiry), } } } +#[cfg(feature = "v2")] impl std::error::Error for CreateRequestError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { use InternalCreateRequestError::*; match &self.0 { Url(error) => Some(error), - #[cfg(feature = "v2")] Hpke(error) => Some(error), - #[cfg(feature = "v2")] OhttpEncapsulation(error) => Some(error), - #[cfg(feature = "v2")] ParseReceiverPubkey(error) => Some(error), - #[cfg(feature = "v2")] MissingOhttpConfig => None, - #[cfg(feature = "v2")] Expired(_) => None, } } } +#[cfg(feature = "v2")] impl From for CreateRequestError { fn from(value: InternalCreateRequestError) -> Self { CreateRequestError(value) } } diff --git a/payjoin/src/send/mod.rs b/payjoin/src/send/mod.rs index 833e7a14..98d6d34a 100644 --- a/payjoin/src/send/mod.rs +++ b/payjoin/src/send/mod.rs @@ -27,10 +27,12 @@ use std::str::FromStr; use bitcoin::hashes::{sha256, Hash}; use bitcoin::psbt::Psbt; use bitcoin::{Amount, FeeRate, Script, ScriptBuf, TxOut, Weight}; -pub use error::{BuildSenderError, CreateRequestError, ResponseError, ValidationError}; -pub(crate) use error::{ - InternalBuildSenderError, InternalCreateRequestError, InternalValidationError, -}; +#[cfg(feature = "v2")] +pub use error::CreateRequestError; +#[cfg(feature = "v2")] +pub(crate) use error::InternalCreateRequestError; +pub use error::{BuildSenderError, ResponseError, ValidationError}; +pub(crate) use error::{InternalBuildSenderError, InternalValidationError}; #[cfg(feature = "v2")] use serde::{Deserialize, Serialize}; use url::Url; @@ -255,15 +257,14 @@ pub struct Sender { impl Sender { /// Extract serialized V1 Request and Context from a Payjoin Proposal - pub fn extract_v1(&self) -> Result<(Request, V1Context), CreateRequestError> { + pub fn extract_v1(&self) -> Result<(Request, V1Context), url::ParseError> { let url = serialize_url( self.endpoint.clone(), self.disable_output_substitution, self.fee_contribution, self.min_fee_rate, "1", // payjoin version - ) - .map_err(InternalCreateRequestError::Url)?; + )?; let body = self.psbt.to_string().as_bytes().to_vec(); Ok(( Request::new_v1(url, body),