From 70e13642ddcd8d8fbdb3ff688247a2c4869205bd Mon Sep 17 00:00:00 2001 From: DanGould Date: Mon, 2 Mar 2026 18:13:53 +0800 Subject: [PATCH] Rename EncapsulationError to DecapsulationError EncapsulationError is a misnomer: it is exclusively raised when decapsulating responses (process_response), never during request encapsulation (create_v2_post_request / create_poll_request, which produce CreateRequestError). Rename to DecapsulationError so the type name matches its actual usage. Also rename the internal variant InternalValidationError:: V2Encapsulation to V2Decapsulation for consistency, and update the display string. Closes #749 partially. --- payjoin-ffi/src/send/error.rs | 10 +++++----- payjoin-ffi/src/send/mod.rs | 4 ++-- payjoin/src/core/send/error.rs | 6 +++--- payjoin/src/core/send/v2/error.rs | 27 +++++++++++++++------------ payjoin/src/core/send/v2/mod.rs | 16 ++++++++-------- 5 files changed, 33 insertions(+), 30 deletions(-) diff --git a/payjoin-ffi/src/send/error.rs b/payjoin-ffi/src/send/error.rs index ed5438cde..792624dd4 100644 --- a/payjoin-ffi/src/send/error.rs +++ b/payjoin-ffi/src/send/error.rs @@ -61,7 +61,7 @@ pub struct CreateRequestError(#[from] send::v2::CreateRequestError); /// Error returned for v2-specific payload encapsulation errors. #[derive(Debug, thiserror::Error, uniffi::Object)] #[error(transparent)] -pub struct EncapsulationError(#[from] send::v2::EncapsulationError); +pub struct DecapsulationError(#[from] send::v2::DecapsulationError); /// Error that may occur when the response from receiver is malformed. #[derive(Debug, thiserror::Error, uniffi::Object)] @@ -122,7 +122,7 @@ pub struct SenderReplayError( pub enum SenderPersistedError { /// rust-payjoin sender Encapsulation error #[error(transparent)] - EncapsulationError(Arc), + DecapsulationError(Arc), /// rust-payjoin sender response error #[error(transparent)] ResponseError(ResponseError), @@ -141,12 +141,12 @@ impl From for SenderPersistedError { fn from(value: ImplementationError) -> Self { SenderPersistedError::Storage(Arc::new(value)) } } -impl From> +impl From> for SenderPersistedError where S: std::error::Error + Send + Sync + 'static, { - fn from(err: payjoin::persist::PersistedError) -> Self { + fn from(err: payjoin::persist::PersistedError) -> Self { if err.storage_error_ref().is_some() { if let Some(storage_err) = err.storage_error() { return SenderPersistedError::from(ImplementationError::new(storage_err)); @@ -154,7 +154,7 @@ where return SenderPersistedError::Unexpected; } if let Some(api_err) = err.api_error() { - return SenderPersistedError::EncapsulationError(Arc::new(api_err.into())); + return SenderPersistedError::DecapsulationError(Arc::new(api_err.into())); } SenderPersistedError::Unexpected } diff --git a/payjoin-ffi/src/send/mod.rs b/payjoin-ffi/src/send/mod.rs index 3bbd11f08..6873d8640 100644 --- a/payjoin-ffi/src/send/mod.rs +++ b/payjoin-ffi/src/send/mod.rs @@ -2,7 +2,7 @@ use std::str::FromStr; use std::sync::{Arc, RwLock}; pub use error::{ - BuildSenderError, CreateRequestError, EncapsulationError, PsbtParseError, ResponseError, + BuildSenderError, CreateRequestError, DecapsulationError, PsbtParseError, ResponseError, SenderInputError, }; @@ -386,7 +386,7 @@ pub struct WithReplyKeyTransition( payjoin::persist::MaybeFatalTransition< payjoin::send::v2::SessionEvent, payjoin::send::v2::Sender, - payjoin::send::v2::EncapsulationError, + payjoin::send::v2::DecapsulationError, >, >, >, diff --git a/payjoin/src/core/send/error.rs b/payjoin/src/core/send/error.rs index 7e70286b5..badbc9dd3 100644 --- a/payjoin/src/core/send/error.rs +++ b/payjoin/src/core/send/error.rs @@ -98,7 +98,7 @@ pub(crate) enum InternalValidationError { ContentTooLarge, Proposal(InternalProposalError), #[cfg(feature = "v2")] - V2Encapsulation(crate::send::v2::EncapsulationError), + V2Decapsulation(crate::send::v2::DecapsulationError), } impl From for ValidationError { @@ -126,7 +126,7 @@ impl fmt::Display for ValidationError { } Proposal(e) => write!(f, "proposal PSBT error: {e}"), #[cfg(feature = "v2")] - V2Encapsulation(e) => write!(f, "v2 encapsulation error: {e}"), + V2Decapsulation(e) => write!(f, "v2 decapsulation error: {e}"), } } } @@ -141,7 +141,7 @@ impl std::error::Error for ValidationError { ContentTooLarge => None, Proposal(e) => Some(e), #[cfg(feature = "v2")] - V2Encapsulation(e) => Some(e), + V2Decapsulation(e) => Some(e), } } } diff --git a/payjoin/src/core/send/v2/error.rs b/payjoin/src/core/send/v2/error.rs index 09fbf7266..8ffd07579 100644 --- a/payjoin/src/core/send/v2/error.rs +++ b/payjoin/src/core/send/v2/error.rs @@ -55,21 +55,24 @@ impl From for CreateRequestError { } } -/// Error returned for v2-specific payload encapsulation errors. +/// Error returned for v2-specific payload decapsulation errors. +/// +/// This error is raised when decapsulating responses from the directory, +/// not during request encapsulation (which produces [`CreateRequestError`]). #[derive(Debug)] -pub struct EncapsulationError(InternalEncapsulationError); +pub struct DecapsulationError(InternalDecapsulationError); #[derive(Debug)] -pub(crate) enum InternalEncapsulationError { +pub(crate) enum InternalDecapsulationError { /// The HPKE failed. Hpke(crate::hpke::HpkeError), /// The directory returned a bad response DirectoryResponse(DirectoryResponseError), } -impl fmt::Display for EncapsulationError { +impl fmt::Display for DecapsulationError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use InternalEncapsulationError::*; + use InternalDecapsulationError::*; match &self.0 { Hpke(error) => write!(f, "HPKE error: {error}"), @@ -78,9 +81,9 @@ impl fmt::Display for EncapsulationError { } } -impl std::error::Error for EncapsulationError { +impl std::error::Error for DecapsulationError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - use InternalEncapsulationError::*; + use InternalDecapsulationError::*; match &self.0 { Hpke(error) => Some(error), @@ -89,12 +92,12 @@ impl std::error::Error for EncapsulationError { } } -impl From for EncapsulationError { - fn from(value: InternalEncapsulationError) -> Self { EncapsulationError(value) } +impl From for DecapsulationError { + fn from(value: InternalDecapsulationError) -> Self { DecapsulationError(value) } } -impl From for super::ResponseError { - fn from(value: InternalEncapsulationError) -> Self { - super::InternalValidationError::V2Encapsulation(value.into()).into() +impl From for super::ResponseError { + fn from(value: InternalDecapsulationError) -> Self { + super::InternalValidationError::V2Decapsulation(value.into()).into() } } diff --git a/payjoin/src/core/send/v2/mod.rs b/payjoin/src/core/send/v2/mod.rs index d2ea8f403..b9b06ba2b 100644 --- a/payjoin/src/core/send/v2/mod.rs +++ b/payjoin/src/core/send/v2/mod.rs @@ -30,8 +30,8 @@ use bitcoin::hashes::{sha256, Hash}; use bitcoin::Address; -pub use error::{CreateRequestError, EncapsulationError}; -use error::{InternalCreateRequestError, InternalEncapsulationError}; +pub use error::{CreateRequestError, DecapsulationError}; +use error::{InternalCreateRequestError, InternalDecapsulationError}; use ohttp::ClientResponse; use serde::{Deserialize, Serialize}; pub use session::{ @@ -342,18 +342,18 @@ impl Sender { self, response: &[u8], post_ctx: ClientResponse, - ) -> MaybeFatalTransition, EncapsulationError> { + ) -> MaybeFatalTransition, DecapsulationError> { match process_post_res(response, post_ctx) { Ok(()) => {} Err(e) => if e.is_fatal() { return MaybeFatalTransition::fatal( SessionEvent::Closed(SessionOutcome::Failure), - InternalEncapsulationError::DirectoryResponse(e).into(), + InternalDecapsulationError::DirectoryResponse(e).into(), ); } else { return MaybeFatalTransition::transient( - InternalEncapsulationError::DirectoryResponse(e).into(), + InternalDecapsulationError::DirectoryResponse(e).into(), ); }, } @@ -483,11 +483,11 @@ impl Sender { if e.is_fatal() { return MaybeSuccessTransitionWithNoResults::fatal( SessionEvent::Closed(SessionOutcome::Failure), - InternalEncapsulationError::DirectoryResponse(e).into(), + InternalDecapsulationError::DirectoryResponse(e).into(), ); } else { return MaybeSuccessTransitionWithNoResults::transient( - InternalEncapsulationError::DirectoryResponse(e).into(), + InternalDecapsulationError::DirectoryResponse(e).into(), ); }, }; @@ -501,7 +501,7 @@ impl Sender { Err(e) => return MaybeSuccessTransitionWithNoResults::fatal( SessionEvent::Closed(SessionOutcome::Failure), - InternalEncapsulationError::Hpke(e).into(), + InternalDecapsulationError::Hpke(e).into(), ), };