Skip to content

Commit

Permalink
Encapsulate internal error variants in tuple
Browse files Browse the repository at this point in the history
No need for them to be named
  • Loading branch information
DanGould committed Jan 7, 2025
1 parent c275329 commit be7df15
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
26 changes: 9 additions & 17 deletions payjoin/src/send/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ impl std::error::Error for BuildSenderError {
/// This is currently opaque type because we aren't sure which variants will stay.
/// You can only display it.
#[derive(Debug)]
pub struct ValidationError {
internal: InternalValidationError,
}
pub struct ValidationError(InternalValidationError);

#[derive(Debug)]
pub(crate) enum InternalValidationError {
Expand All @@ -100,24 +98,22 @@ pub(crate) enum InternalValidationError {
}

impl From<InternalValidationError> for ValidationError {
fn from(value: InternalValidationError) -> Self { ValidationError { internal: value } }
fn from(value: InternalValidationError) -> Self { ValidationError(value) }
}

impl From<crate::psbt::AddressTypeError> for ValidationError {
fn from(value: crate::psbt::AddressTypeError) -> Self {
ValidationError {
internal: InternalValidationError::Proposal(InternalProposalError::InvalidAddressType(
value,
)),
}
ValidationError(InternalValidationError::Proposal(
InternalProposalError::InvalidAddressType(value),
))
}
}

impl fmt::Display for ValidationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use InternalValidationError::*;

match &self.internal {
match &self.0 {
Parse => write!(f, "couldn't decode as PSBT or JSON",),
Io(e) => write!(f, "couldn't read PSBT: {}", e),
Proposal(e) => write!(f, "proposal PSBT error: {}", e),
Expand All @@ -131,7 +127,7 @@ impl std::error::Error for ValidationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use InternalValidationError::*;

match &self.internal {
match &self.0 {
Parse => None,
Io(error) => Some(error),
Proposal(e) => Some(e),
Expand Down Expand Up @@ -329,16 +325,12 @@ impl From<WellKnownError> for ResponseError {
}

impl From<InternalValidationError> for ResponseError {
fn from(value: InternalValidationError) -> Self {
Self::Validation(ValidationError { internal: value })
}
fn from(value: InternalValidationError) -> Self { Self::Validation(ValidationError(value)) }
}

impl From<InternalProposalError> for ResponseError {
fn from(value: InternalProposalError) -> Self {
ResponseError::Validation(ValidationError {
internal: InternalValidationError::Proposal(value),
})
ResponseError::Validation(ValidationError(InternalValidationError::Proposal(value)))
}
}

Expand Down
10 changes: 4 additions & 6 deletions payjoin/src/send/v2/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ impl From<ParseReceiverPubkeyParamError> for CreateRequestError {

/// Error returned for v2-specific payload encapsulation errors.
#[derive(Debug)]
pub struct EncapsulationError {
internal: InternalEncapsulationError,
}
pub struct EncapsulationError(InternalEncapsulationError);

#[derive(Debug)]
pub(crate) enum InternalEncapsulationError {
Expand All @@ -83,7 +81,7 @@ impl fmt::Display for EncapsulationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use InternalEncapsulationError::*;

match &self.internal {
match &self.0 {
InvalidSize(size) => write!(f, "invalid size: {}", size),
UnexpectedStatusCode(status) => write!(f, "unexpected status code: {}", status),
Ohttp(error) => write!(f, "OHTTP encapsulation error: {}", error),
Expand All @@ -96,7 +94,7 @@ impl std::error::Error for EncapsulationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use InternalEncapsulationError::*;

match &self.internal {
match &self.0 {
InvalidSize(_) => None,
UnexpectedStatusCode(_) => None,
Ohttp(error) => Some(error),
Expand All @@ -106,7 +104,7 @@ impl std::error::Error for EncapsulationError {
}

impl From<InternalEncapsulationError> for EncapsulationError {
fn from(value: InternalEncapsulationError) -> Self { EncapsulationError { internal: value } }
fn from(value: InternalEncapsulationError) -> Self { EncapsulationError(value) }
}

impl From<InternalEncapsulationError> for super::ResponseError {
Expand Down

0 comments on commit be7df15

Please sign in to comment.