Skip to content

Commit ec4b67e

Browse files
committed
Refactor DirectoryResponseError mappings
Instead of mapping DirectoryResponseError to the corresponding variants on InternalSessionError and InternalCreateRequestError, nest those variants by replacing them with DirectoryResponseError.
1 parent 91e854e commit ec4b67e

File tree

5 files changed

+51
-72
lines changed

5 files changed

+51
-72
lines changed

payjoin/src/ohttp.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,42 @@ pub fn ohttp_encapsulate(
5252
Ok((buffer, ohttp_ctx))
5353
}
5454

55+
#[derive(Debug)]
5556
pub enum DirectoryResponseError {
5657
InvalidSize(usize),
5758
OhttpDecapsulation(OhttpEncapsulationError),
5859
UnexpectedStatusCode(http::StatusCode),
5960
}
6061

62+
impl fmt::Display for DirectoryResponseError {
63+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64+
use DirectoryResponseError::*;
65+
66+
match self {
67+
OhttpDecapsulation(e) => write!(f, "OHTTP Decapsulation Error: {e}"),
68+
InvalidSize(size) => write!(
69+
f,
70+
"Unexpected response size {}, expected {} bytes",
71+
size,
72+
crate::directory::ENCAPSULATED_MESSAGE_BYTES
73+
),
74+
UnexpectedStatusCode(status) => write!(f, "Unexpected status code: {status}"),
75+
}
76+
}
77+
}
78+
79+
impl error::Error for DirectoryResponseError {
80+
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
81+
use DirectoryResponseError::*;
82+
83+
match self {
84+
OhttpDecapsulation(e) => Some(e),
85+
InvalidSize(_) => None,
86+
UnexpectedStatusCode(_) => None,
87+
}
88+
}
89+
}
90+
6191
pub fn process_get_res(
6292
res: &[u8],
6393
ohttp_context: ohttp::ClientResponse,

payjoin/src/receive/v2/error.rs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,6 @@ impl From<InternalSessionError> for Error {
2121
fn from(e: InternalSessionError) -> Self { V2(e.into()) }
2222
}
2323

24-
impl From<DirectoryResponseError> for SessionError {
25-
fn from(value: DirectoryResponseError) -> Self {
26-
match value {
27-
DirectoryResponseError::InvalidSize(e) =>
28-
InternalSessionError::UnexpectedResponseSize(e),
29-
DirectoryResponseError::OhttpDecapsulation(e) =>
30-
InternalSessionError::OhttpEncapsulation(e),
31-
DirectoryResponseError::UnexpectedStatusCode(e) =>
32-
InternalSessionError::UnexpectedStatusCode(e),
33-
}
34-
.into()
35-
}
36-
}
37-
38-
impl From<DirectoryResponseError> for Error {
39-
fn from(value: DirectoryResponseError) -> Self { V2(value.into()) }
40-
}
41-
4224
#[derive(Debug)]
4325
pub(crate) enum InternalSessionError {
4426
/// Url parsing failed
@@ -49,10 +31,8 @@ pub(crate) enum InternalSessionError {
4931
OhttpEncapsulation(OhttpEncapsulationError),
5032
/// Hybrid Public Key Encryption failed
5133
Hpke(HpkeError),
52-
/// Unexpected response size
53-
UnexpectedResponseSize(usize),
54-
/// Unexpected status code
55-
UnexpectedStatusCode(http::StatusCode),
34+
/// The directory returned a bad response
35+
DirectoryResponse(DirectoryResponseError),
5636
}
5737

5838
impl From<OhttpEncapsulationError> for Error {
@@ -74,13 +54,7 @@ impl fmt::Display for SessionError {
7454
Expired(expiry) => write!(f, "Session expired at {expiry:?}"),
7555
OhttpEncapsulation(e) => write!(f, "OHTTP Encapsulation Error: {e}"),
7656
Hpke(e) => write!(f, "Hpke decryption failed: {e}"),
77-
UnexpectedResponseSize(size) => write!(
78-
f,
79-
"Unexpected response size {}, expected {} bytes",
80-
size,
81-
crate::directory::ENCAPSULATED_MESSAGE_BYTES
82-
),
83-
UnexpectedStatusCode(status) => write!(f, "Unexpected status code: {status}"),
57+
DirectoryResponse(e) => write!(f, "Directory response error: {e}"),
8458
}
8559
}
8660
}
@@ -94,8 +68,7 @@ impl error::Error for SessionError {
9468
Expired(_) => None,
9569
OhttpEncapsulation(e) => Some(e),
9670
Hpke(e) => Some(e),
97-
UnexpectedResponseSize(_) => None,
98-
UnexpectedStatusCode(_) => None,
71+
DirectoryResponse(e) => Some(e),
9972
}
10073
}
10174
}

payjoin/src/receive/v2/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ impl Receiver<WithContext> {
182182
body: &[u8],
183183
context: ohttp::ClientResponse,
184184
) -> Result<Option<Receiver<UncheckedProposal>>, Error> {
185-
let body = match process_get_res(body, context)? {
185+
let body = match process_get_res(body, context)
186+
.map_err(InternalSessionError::DirectoryResponse)?
187+
{
186188
Some(body) => body,
187189
None => return Ok(None),
188190
};
@@ -344,7 +346,8 @@ impl Receiver<UncheckedProposal> {
344346
body: &[u8],
345347
context: ohttp::ClientResponse,
346348
) -> Result<(), SessionError> {
347-
process_post_res(body, context).map_err(Into::into)
349+
process_post_res(body, context)
350+
.map_err(|e| InternalSessionError::DirectoryResponse(e).into())
348351
}
349352
}
350353

@@ -633,7 +636,8 @@ impl Receiver<PayjoinProposal> {
633636
res: &[u8],
634637
ohttp_context: ohttp::ClientResponse,
635638
) -> Result<(), Error> {
636-
process_post_res(res, ohttp_context).map_err(Into::into)
639+
process_post_res(res, ohttp_context)
640+
.map_err(|e| InternalSessionError::DirectoryResponse(e).into())
637641
}
638642
}
639643

payjoin/src/send/v2/error.rs

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use core::fmt;
22

3-
use super::ResponseError;
43
use crate::ohttp::DirectoryResponseError;
54
use crate::uri::url_ext::ParseReceiverPubkeyParamError;
65

@@ -75,25 +74,19 @@ pub struct EncapsulationError(InternalEncapsulationError);
7574

7675
#[derive(Debug)]
7776
pub(crate) enum InternalEncapsulationError {
78-
/// The response size is not the expected size.
79-
InvalidSize(usize),
80-
/// The status code is not the expected status code.
81-
UnexpectedStatusCode(http::StatusCode),
8277
/// The HPKE failed.
8378
Hpke(crate::hpke::HpkeError),
84-
/// The encapsulation failed.
85-
Ohttp(crate::ohttp::OhttpEncapsulationError),
79+
/// The directory returned a bad response
80+
DirectoryResponse(DirectoryResponseError),
8681
}
8782

8883
impl fmt::Display for EncapsulationError {
8984
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9085
use InternalEncapsulationError::*;
9186

9287
match &self.0 {
93-
InvalidSize(size) => write!(f, "invalid size: {size}"),
94-
UnexpectedStatusCode(status) => write!(f, "unexpected status code: {status}"),
95-
Ohttp(error) => write!(f, "OHTTP encapsulation error: {error}"),
9688
Hpke(error) => write!(f, "HPKE error: {error}"),
89+
DirectoryResponse(e) => write!(f, "Directory response error: {e}"),
9790
}
9891
}
9992
}
@@ -103,10 +96,8 @@ impl std::error::Error for EncapsulationError {
10396
use InternalEncapsulationError::*;
10497

10598
match &self.0 {
106-
InvalidSize(_) => None,
107-
UnexpectedStatusCode(_) => None,
108-
Ohttp(error) => Some(error),
10999
Hpke(error) => Some(error),
100+
DirectoryResponse(e) => Some(e),
110101
}
111102
}
112103
}
@@ -117,28 +108,6 @@ impl From<InternalEncapsulationError> for EncapsulationError {
117108

118109
impl From<InternalEncapsulationError> for super::ResponseError {
119110
fn from(value: InternalEncapsulationError) -> Self {
120-
super::ResponseError::Validation(
121-
super::InternalValidationError::V2Encapsulation(value.into()).into(),
122-
)
123-
}
124-
}
125-
126-
impl From<DirectoryResponseError> for EncapsulationError {
127-
fn from(value: DirectoryResponseError) -> Self {
128-
match value {
129-
DirectoryResponseError::InvalidSize(e) => InternalEncapsulationError::InvalidSize(e),
130-
DirectoryResponseError::OhttpDecapsulation(e) => InternalEncapsulationError::Ohttp(e),
131-
DirectoryResponseError::UnexpectedStatusCode(e) =>
132-
InternalEncapsulationError::UnexpectedStatusCode(e),
133-
}
134-
.into()
135-
}
136-
}
137-
138-
impl From<DirectoryResponseError> for ResponseError {
139-
fn from(value: DirectoryResponseError) -> Self {
140-
ResponseError::Validation(
141-
super::InternalValidationError::V2Encapsulation(value.into()).into(),
142-
)
111+
super::InternalValidationError::V2Encapsulation(value.into()).into()
143112
}
144113
}

payjoin/src/send/v2/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ impl Sender<V2PostContext> {
325325
self,
326326
response: &[u8],
327327
) -> Result<Sender<V2GetContext>, EncapsulationError> {
328-
process_post_res(response, self.state.ohttp_ctx)?;
328+
process_post_res(response, self.state.ohttp_ctx)
329+
.map_err(InternalEncapsulationError::DirectoryResponse)?;
329330
Ok(Sender {
330331
state: V2GetContext {
331332
endpoint: self.state.endpoint,
@@ -394,7 +395,9 @@ impl Sender<V2GetContext> {
394395
response: &[u8],
395396
ohttp_ctx: ohttp::ClientResponse,
396397
) -> Result<Option<Psbt>, ResponseError> {
397-
let body = match process_get_res(response, ohttp_ctx)? {
398+
let body = match process_get_res(response, ohttp_ctx)
399+
.map_err(InternalEncapsulationError::DirectoryResponse)?
400+
{
398401
Some(body) => body,
399402
None => return Ok(None),
400403
};

0 commit comments

Comments
 (0)